当您具有简单的条件和可能复杂的主体时,条件结构很容易编写:
if simple_condition_expressed_in_one_liner complicated_body_that_may_be_long complicated_body_that_may_be_long complicated_body_that_may_be_long end
if condition1 and condition2 and condition3 and some_more_complicated_condition_that_cannot_be_written_on_a_single_line and still_some_more_complicated_condition_that_cannot_be_written_on_a_single_line simple_body end
解决方法
我总是尝试重构为较小的描述性方法.
使用您的示例,而不是:
until (print "Username : "; gets.chomp == "user") and (print "Password : "; gets.chomp == "pa$$word") puts "Error,incorrect details" end
我会用:
def correct_user print "Username : " gets.chomp == "user" end def correct_password print "Password : " gets.chomp == "pa$$word" end until correct_user and correct_password puts "Error,incorrect details" end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。