我是Ruby的新手,我在Ruby的Poignant Guide之后遇到了一个问题:
这个表达式是否返回true?
2005..2009 === 2007
但我不知道为什么我从以下代码中收到此警告消息
wishTraditional.rb:4: warning: integer literal in conditional range
Run Code Online (Sandbox Code Playgroud)
码:
def makTimeLine(year)
if 1984 === year
"Born."
elsif 2005..2009 === year
"Sias."
else
"Sleeping"
end
end
puts makTimeLine(2007)
Run Code Online (Sandbox Code Playgroud)
并且它返回睡眠,这是错误的,应该是西亚斯
BTW这两个点是什么意思?如何找到有关它的更多信息?
Bar*_*min 11
我认为你最好使用这样的东西:
elsif (2005..2009).include?(year)
Run Code Online (Sandbox Code Playgroud)
这是关于Ruby范围的文档
更新:如果您坚持使用===,则应将范围括在括号中:
elseif (2005..2009) === year
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5375 次 |
| 最近记录: |