Skip to content

Instantly share code, notes, and snippets.

@therealadam
Created December 9, 2009 20:48
Show Gist options
  • Save therealadam/252797 to your computer and use it in GitHub Desktop.
Save therealadam/252797 to your computer and use it in GitHub Desktop.
class Array
def map_e(elz, &block)
results = []
if length > 0
each do |o|
block.call(o)
end
else
elz.call
end
end
end
# Ruby 1.9-style lambdas make this read a little better
puts %w{dog cat zebra}.map_e(-> { 'No animals!' }) { |o| o.upcase }
puts [].map_e(->{ 'No animals' }) { |o| o.upcase }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment