Skip to content

Instantly share code, notes, and snippets.

@sagarbommidi
Created November 4, 2016 09:04
Show Gist options
  • Save sagarbommidi/1474154e096fdbe823c08c1ba34f4ce6 to your computer and use it in GitHub Desktop.
Save sagarbommidi/1474154e096fdbe823c08c1ba34f4ce6 to your computer and use it in GitHub Desktop.
class Array
def my_map
result = []
return to_enum :my_map unless block_given?
each { |s| result << yield(s) }
result
end
end
a = [1, 2, 3, 4]
b = a.my_map do |x|
x + 1
end
c = a.my_map(&:to_s)
puts b.inspect #=> [2, 3, 4, 5]
puts c.inspect #=> ["1", "2", "3", "4"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment