Skip to content

Instantly share code, notes, and snippets.

@gaga5lala
Created May 19, 2017 03:03
Show Gist options
  • Save gaga5lala/2d6b36f9c211770e52d21978d41cac9e to your computer and use it in GitHub Desktop.
Save gaga5lala/2d6b36f9c211770e52d21978d41cac9e to your computer and use it in GitHub Desktop.
Reimplement ruby map
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