Created
October 15, 2014 15:16
-
-
Save DiegoSalazar/8f716a2e97e6fbfe63f4 to your computer and use it in GitHub Desktop.
Ruby each with else
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def for_each(items, do_this, options = { else: -> {} }) | |
unless items.empty? | |
items.each &do_this | |
else | |
options[:else].call | |
end | |
end | |
items = [1,2,3] | |
for_each items, ->(i) { | |
p i | |
}, else: -> { | |
p 'empty' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment