Skip to content

Instantly share code, notes, and snippets.

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 AlexParamonov/1647601 to your computer and use it in GitHub Desktop.
Save AlexParamonov/1647601 to your computer and use it in GitHub Desktop.
This gist will lich all your free time, so be warned!
class Q
def add
puts "added to Q"
end
end
queue = Q.new
notifier = Module.new do
def add(*args)
super
puts "i am from notifier!"
end
alias_method :push, :add
alias_method :<<, :add
end
queue.send :extend, notifier
# Ok, lets try:
queue.add
# Ruby 1.9
# added to Q
# i am from notifier!
# Ruby 1.8
# added to Q
# i am from notifier!
# what about our aliases?
queue.push
# Ruby 1.9
# added to Q
# i am from notifier!
# Ruby 1.8
# test.rb:11:in `push': super: no superclass method `add' for #<Q:0x1dbf0f0> (NoMethodError)
# from test.rb:19
# WTF?!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment