Created
November 4, 2016 09:04
-
-
Save sagarbommidi/1474154e096fdbe823c08c1ba34f4ce6 to your computer and use it in GitHub Desktop.
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
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