我正在阅读 Michael Hartl 的 Ruby on Rails 3.2 教程,我很困惑为什么第 4.1.1 节中的标题助手没有失败。
他谈到如果您从视图中遗漏了这段代码,则需要一个标题助手:
<% provide(:title, 'Home') %>
但是在应用程序布局文件中有这一行:
<title><%= full_title(yield(:title)) %></title>
由于提供没有为 :title 符号设置值,这不会将 nil 值传递给 full_title 帮助程序吗?
在本章的后面,他有一个输入到 rails 控制台的示例,该示例与 full_title 函数相同:
def string_message(string)
if string.empty?
"It's an empty string!"
else
"The string is nonempty."
end
end
这让我更加困惑。
在控制台,如果我输入:
string_message("")
然后我得到"It's an empty string!"
string_message("something")
然后我得到"The string is nonempty."
string_message(nil)
然后我得到NoMethodError: undefined method 'empty?' for nil:NilClass
string_message(test)
然后我得到ArgumentError: wrong number of arguments (0 for 2..3)
string_message(:test)
然后我得到"The string is nonempty."
那么传递一个未定义的符号不会产生一个零值吗?但它也是非空的?为什么 :title 不被视为非空?如果有人可以为我解决这个问题,那就太好了。