无法解决 Rails 控制台中的 if-else 循环(Rails 教程 - 第 4 章)
Cannot resolve if-else loop in Rails console (Rails Tutorial - Ch 4)
我正在关注 Micheal Hartl 的 Rails 教程,这个问题来自第 4 章。
我正在尝试执行以下循环:
>> s = "foobar"
>> if s.nil?
>> "The variable is nil"
>> elsif s.empty?
>> "The string is empty"
>> elsif s.include?("foo")
>> "The string includes 'foo'"
>> end
根据书应该打印:
=> "The string includes 'foo'"
但我得到了答案:nil
现在我也一个一个地尝试了分支(即just if条件)然后它工作正常。
尝试了下面回答的几件事,但遇到了同样的问题。
> if s.nil?
> return_value = "The variable is nil"
> elsif s.empty?
> return_value = "The string is empty?"
> elseif s.include?("foo")
> return_value = "The string has 'foo'"
> end
=> nil
我一直在使用 "elseif" 而不是 "elsif"
已更新(忽略下面的评论讨论):
if-else 循环(或与此相关的任何循环)将在控制台中 return nil。循环所做的只是为您设置变量,但实际上并没有 return 它们。
因此,对于 return 他们,我们需要将循环包装在这样的方法中:
[8] pry(main)> def my_loop
[8] pry(main)* return_value = nil
[8] pry(main)* s = "foobar"
[8] pry(main)* if s.nil?
[8] pry(main)* return_value = "The variable is nil"
[8] pry(main)* elsif s.empty?
[8] pry(main)* return_value = "The string is empty?"
[8] pry(main)* elsif s.include?("foo")
[8] pry(main)* return_value = "The string includes 'foo'"
[8] pry(main)* end
[8] pry(main)* return_value
[8] pry(main)* end
=> :my_loop
[9] pry(main)> my_loop
=> "The string includes 'foo'"
[10] pry(main)>
您实际上并没有在任何条件下返回任何东西。您需要有一个 puts
语句,它将打印字符串。
你的方式:
[3] pry(main)> if s.nil?
[3] pry(main)* "blah"
[3] pry(main)* elsif s.empty?
[3] pry(main)* "tree"
[3] pry(main)* elsif s.include?("tree")
[3] pry(main)* "ha"
[3] pry(main)* end
=> nil
我的方式:
[1] pry(main)> s = "foobar"
=> "foobar"
[2] pry(main)> if s.nil?
[2] pry(main)* puts "blah"
[2] pry(main)* elsif s.empty?
[2] pry(main)* puts "blah"
[2] pry(main)* elsif s.include?("foo")
[2] pry(main)* puts "tree"
[2] pry(main)* end
tree
=> nil
我找到了这个案例的答案:
elseif 不是正确的分支命令。所以你使用的代码实际上被解释为:
if s.nil?
return_value = "The variable is nil"
elsif s.empty?
return_value = "The string is empty?"
elseif s.include?("foo")
return_value = "The string has 'foo'"
end
为什么它 return 为零? 因为代码实际上落在 else
块中,如您所见:not nil and not empty
为什么 elseif 不会 return 错误? 因为 Ruby 使用 惰性求值 。 s.empty?
return false,所以它甚至不尝试执行 elsif
中的块。那么它就不知道elseif函数是否是预定义的。
这样可以理解吗?
所以解决方法很简单,将elseif改成elsif。玩得开心!
我正在关注 Micheal Hartl 的 Rails 教程,这个问题来自第 4 章。
我正在尝试执行以下循环:
>> s = "foobar"
>> if s.nil?
>> "The variable is nil"
>> elsif s.empty?
>> "The string is empty"
>> elsif s.include?("foo")
>> "The string includes 'foo'"
>> end
根据书应该打印:
=> "The string includes 'foo'"
但我得到了答案:nil
现在我也一个一个地尝试了分支(即just if条件)然后它工作正常。
尝试了下面回答的几件事,但遇到了同样的问题。
> if s.nil?
> return_value = "The variable is nil"
> elsif s.empty?
> return_value = "The string is empty?"
> elseif s.include?("foo")
> return_value = "The string has 'foo'"
> end
=> nil
我一直在使用 "elseif" 而不是 "elsif"
已更新(忽略下面的评论讨论):
if-else 循环(或与此相关的任何循环)将在控制台中 return nil。循环所做的只是为您设置变量,但实际上并没有 return 它们。
因此,对于 return 他们,我们需要将循环包装在这样的方法中:
[8] pry(main)> def my_loop
[8] pry(main)* return_value = nil
[8] pry(main)* s = "foobar"
[8] pry(main)* if s.nil?
[8] pry(main)* return_value = "The variable is nil"
[8] pry(main)* elsif s.empty?
[8] pry(main)* return_value = "The string is empty?"
[8] pry(main)* elsif s.include?("foo")
[8] pry(main)* return_value = "The string includes 'foo'"
[8] pry(main)* end
[8] pry(main)* return_value
[8] pry(main)* end
=> :my_loop
[9] pry(main)> my_loop
=> "The string includes 'foo'"
[10] pry(main)>
您实际上并没有在任何条件下返回任何东西。您需要有一个 puts
语句,它将打印字符串。
你的方式:
[3] pry(main)> if s.nil?
[3] pry(main)* "blah"
[3] pry(main)* elsif s.empty?
[3] pry(main)* "tree"
[3] pry(main)* elsif s.include?("tree")
[3] pry(main)* "ha"
[3] pry(main)* end
=> nil
我的方式:
[1] pry(main)> s = "foobar"
=> "foobar"
[2] pry(main)> if s.nil?
[2] pry(main)* puts "blah"
[2] pry(main)* elsif s.empty?
[2] pry(main)* puts "blah"
[2] pry(main)* elsif s.include?("foo")
[2] pry(main)* puts "tree"
[2] pry(main)* end
tree
=> nil
我找到了这个案例的答案:
elseif 不是正确的分支命令。所以你使用的代码实际上被解释为:
if s.nil?
return_value = "The variable is nil"
elsif s.empty?
return_value = "The string is empty?"
elseif s.include?("foo")
return_value = "The string has 'foo'"
end
为什么它 return 为零? 因为代码实际上落在 else
块中,如您所见:not nil and not empty
为什么 elseif 不会 return 错误? 因为 Ruby 使用 惰性求值 。 s.empty?
return false,所以它甚至不尝试执行 elsif
中的块。那么它就不知道elseif函数是否是预定义的。
这样可以理解吗?
所以解决方法很简单,将elseif改成elsif。玩得开心!