Skip to content

Instantly share code, notes, and snippets.

@lantins
Created January 4, 2012 06:00
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 lantins/1558714 to your computer and use it in GitHub Desktop.
Save lantins/1558714 to your computer and use it in GitHub Desktop.
class User
attr_accessor :products
def initialize
@products = ProductsArray.new
end
end
class ProductsArray < Array
def <<(obj)
super(obj)
puts "you've pushed an object onto the array"
end
end
usr = User.new
usr.products << 'test'
p usr
@sj26
Copy link

sj26 commented Jan 4, 2012

You should probably use:

  def <<(obj)
    super(obj).tap do
      puts "you've pushed an object onto the array"
    end
  end

so products << 1 << 2 still works, etc.

@lantins
Copy link
Author

lantins commented Jan 4, 2012

Aha! I wouldn't of known that, thanks =)

Link to docs: http://ruby-doc.org/core-1.9.3/Object.html#method-i-tap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment