Skip to content

Instantly share code, notes, and snippets.

@DiegoSalazar
Created October 15, 2014 15:16
Show Gist options
  • Save DiegoSalazar/8f716a2e97e6fbfe63f4 to your computer and use it in GitHub Desktop.
Save DiegoSalazar/8f716a2e97e6fbfe63f4 to your computer and use it in GitHub Desktop.
Ruby each with else
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