Created
July 24, 2012 19:30
-
-
Save rubysolo/3172105 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'debugger' | |
class Node | |
def to_s | |
"new hotness" | |
end | |
end | |
class Module | |
def method_added(method_id) | |
puts "added method #{ method_id } to #{ self } (which is a #{ self.class })." | |
if self.ancestors.include?(Node) && method_id == :to_s && ! self.respond_to?(:hooked?) | |
puts " ==> redefining..." | |
self.class_eval do | |
alias_method :original_to_s, :to_s | |
def self.hooked? | |
true | |
end | |
def to_s(*args) | |
super(*args) + ' ' + original_to_s | |
end | |
end | |
end | |
end | |
end | |
class SubNode < Node | |
def to_s | |
"old & busted" | |
end | |
end | |
puts Node.new | |
puts SubNode.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment