Created
May 19, 2017 03:03
-
-
Save gaga5lala/2d6b36f9c211770e52d21978d41cac9e to your computer and use it in GitHub Desktop.
Reimplement ruby map
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 gaga_map(&blk) | |
puts "---method #{__method__} start---" | |
output = [] | |
if block_given? | |
self.each do |item| | |
output << blk.call(item) | |
end | |
output = output.to_enum :gaga_map | |
else | |
output = self.to_enum :gaga_map | |
end | |
output | |
end | |
end | |
arr = ["a", "b", "c"] | |
ar1 = [1,2,3,4] | |
puts "==map==" | |
p arr.map(&:upcase) | |
p arr.map | |
puts "==gaga_map==" | |
p arr.gaga_map(&:upcase) | |
p = arr.gaga_map |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment