Skip to content

Instantly share code, notes, and snippets.

@sagarbommidi
Created November 7, 2016 11:30
Show Gist options
  • Save sagarbommidi/fae53fa6a8bccaeed13584ac3e3846c7 to your computer and use it in GitHub Desktop.
Save sagarbommidi/fae53fa6a8bccaeed13584ac3e3846c7 to your computer and use it in GitHub Desktop.
class Array
def my_map
return to_enum :my_map unless block_given?
inject([]) { |result, value| result << yield(value) }
end
end
a = [1, 2, 3, 4]
b = a.my_map do |x|
x + 1
end
c = a.my_map(&:to_s)
d = a.my_map # when no block has given
puts b.inspect #=> [2, 3, 4, 5]
puts c.inspect #=> ["1", "2", "3", "4"]
puts d.inspect #=> #<Enumerator: [1, 2, 3, 4]:my_map>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment