Skip to content

Instantly share code, notes, and snippets.

@hogihung
Created March 16, 2014 19:31
Show Gist options
  • Save hogihung/9588535 to your computer and use it in GitHub Desktop.
Save hogihung/9588535 to your computer and use it in GitHub Desktop.
Practice with blocks - new_map method
class Array
def new_map
a = []
self.each do |item|
a << yield(item)
end
a
end
def new_map!(&block)
a = self
b = self.new_map(&block)
a.replace(b)
end
end
a = [1, 2, 3]
a.new_map! { |i| i + 1 }
p a
b = [1, "two", :three]
b.new_map! { |i| i.class }
p b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment