我有这个代码:
def with_else
puts 'we enter something funny'
if true
puts "yes"
'return YES'
end
'return what?'
else
puts 'no'
'return else -> no'
end
puts with_else
输出是这样的:
we enter something funny
yes
no
return else -> no
为什么我没有收到错误消息?什么是def
/else
/end
mean?
http://rubyfiddle.com/riddles/8df07 http://rubyfiddle.com/riddles/8df07
这实际上是一个更大的语法结构的一部分:
def foo
# method stuff
rescue
# stuff if an exception is caught
else
# stuff if no exception is caught
ensure
# always run after rescue and else
end
但每个部分都是可选的!所以你可以省略rescue
and ensure
得到你的例子。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)