应该是一个容易的.我想,从阅读
this blog post,我可以在我的下一个命令之后返回一些东西:
next“new value”如果axis_range ==“test”
我真正想做的是在同一行记录下一个原因:
next @ logger.info(‘跳过这个项目,因为有趣’)除非(elephants.size> 0)
我在ruby doc没有找到关于下一个用法的讨论.代码肯定有效.我意识到我可以用一个除非块来做,但那行代码是sooo简洁.
两个问题:
解决方法
” Like the
return
andbreak
keywords,next
may be used alone,or
it may be followed by an expression or a comma-separated list of
expressions. Whennext
is used in a loop,any values following
next
are ignored. In a block however the expression or expressions
become the “return value” of theyield
statement that invoked the
block.” (The Ruby Programming Language,David Flanagan & Yukihiro Matsumoto,2008,page 150)
这本书给出了这个例子:
squareroots = data.collect do |x| next 0 if x < 0 # return 0 for negative values Math.sqrt(x) end
和这个选择:
squareroots = data.collect do |x| if (x < 0) then 0 else Math.sqrt(x) end end