I'm trying to write some Ruby code that will return a value from a block that is passed to a function. I don't want the block to exit early, I just want to avoid writing too many if
statements in order to implicitly return the value.
How can I rewrite this to return the value early?
This works:
def my_method
my_array.delete_if do |item|
if item.removed?
true
elsif item == 'something else'
true
else
false
end
end
end
This won't work because you can't call return in a block:
def my_method
my_array.delete_if do |item|
return true if item.removed?
return true if item == 'something else'
# ...
end
end