Created
April 25, 2011 11:12
-
-
Save massive/940375 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
module Test | |
class One | |
def instance | |
puts "instance" | |
end | |
def self.class_meth | |
puts "class" | |
end | |
end | |
end | |
module Override | |
module Test | |
def self.included(base) | |
base.send :extend, ClassMethods | |
base.send :include, InstanceMethods | |
base.class_eval do | |
alias_method :old_instance, :instance | |
alias_method :instance, :new_instance | |
class << self | |
alias_method :old_class_meth, :class_meth | |
alias_method :class_meth, :new_class_meth | |
end | |
end | |
end | |
module InstanceMethods | |
def new_instance | |
puts "overridden instance" | |
end | |
end | |
module ClassMethods | |
def new_class_meth | |
puts "overridden class_meth" | |
end | |
end | |
end | |
end | |
Test::One.send(:include, Override::Test) | |
Test::One.new.instance # => "overridden instance" | |
Test::One.class_meth # => "overridden class_meth" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment