Should be an easy one. I thought, from reading this blog post that I could return something right after my next
command:
next "new value" if axis_range == "test"
What I'd really like to do is log the reason for the next on the same line:
next @logger.info('skipping this item because for fun') unless (elephants.size > 0)
I can't find any discussion of this usage of next
on ruby doc. The code certainly works. I realize I can do this with an unless
block but the that line of code is sooo concise.
Two questions:
next
a little odd and not 'ruby-ish'? " 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 followingnext
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)
The book gives this example:
squareroots = data.collect do |x|
next 0 if x < 0 # return 0 for negative values
Math.sqrt(x)
end
and this alternative:
squareroots = data.collect do |x|
if (x < 0) then 0 else Math.sqrt(x) end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With