Created
November 7, 2016 11:30
-
-
Save sagarbommidi/fae53fa6a8bccaeed13584ac3e3846c7 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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