Skip to content

Instantly share code, notes, and snippets.

@sj26
Created January 4, 2012 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sj26/1558806 to your computer and use it in GitHub Desktop.
Save sj26/1558806 to your computer and use it in GitHub Desktop.
a = []
def a.<< e
super.tap do
puts "Adding #{e.inspect}"
end
end
a << 1
# Adding 1
# => [1]
class Blah
def initialize
@a = []
def @a.<< e
super.tap do
puts "Adding #{e.inspect}"
end
end
end
end
blah = Blah.new
blah.instance_eval do
@a << 2
end
# Adding 2
# => [2]
class User
module ProductArrayMixin
def << e
super.tap do
puts "Adding #{e.inspect}"
end
end
end
attr_reader :products
def initialize
@products = []
@products.extend ProductArrayMixin
end
end
user = User.new
user.products << 3
# Adding 3
# => [3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment