foo.each do |x|
return if x == 3
end
Don't "return" out of the enclosing function.The thing is, they’re rare in practice for cultural reasons.
What are the "hacky" methods currently used for packaging you mentioned? The only methods I'm aware of no longer work or still require a separate runtime.
In Ruby iteration is implemented by letting something call a block repeatedly until the end of the iteration. The interpreter provides jump points in order to implement skipping or breaking the iteration. A continue is implemented as a form of jumping to the end of the block, a break is implemented by jumping past the call to the iterator function. Without the return it would be very awkward to return something from the function.Imagine a function that returns the first even item from a list:
>> def find_even iterable
>> iterable.each { |x| return x if x % 2 == 0 }
>> end
=> nil
>> find_even [1, 3, 5, 6]
=> 6
Imagine the non-local return was not available, you would have to rewrite it like this: >> def find_even iterable
>> done = false
>> rv = nil
>> iterable.each { |x|
>> if x % 2 == 0
>> done = true
>> rv = x
>> break
>> end
>> }
>> rv
>> end
*Blocks are pretty much an iteration construct.
It's really not that confusing.
"And finally, pie in the sky, can we solve packaging apps up into distributable binaries, please? Rust and Go are making us look bad."
As a ruby programmer the lack of any reasonable - i.e. not hacky - way to build binaries is annoying. Unless you're building a server side app, you can pretty much forget using ruby because of this. Maybe that's all ruby cares about, its certainly its niche but I like ruby and would like to be able to use it for cli programs, etc. Personally, I don't want end users of my code to have to install ruby, learn about ruby gems and boot the ruby vm before having to run a cli program.