-
-
Save rkh/343586 to your computer and use it in GitHub Desktop.
This file contains 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
class Object | |
def self.method_added(name) | |
const_set :InstanceMethods, Module.new unless const_defined? :InstanceMethods | |
meth = instance_method(name).bind(self.allocate) | |
self::InstanceMethods.send(:define_method, name, &meth) | |
remove_method(name) | |
include self::InstanceMethods unless ancestors.include? self::InstanceMethods | |
end | |
end | |
################## END MAGIC ############## | |
class X | |
def x; "X" end | |
end | |
module Y | |
def x; super + "Y" end | |
end | |
puts X.new.x | |
class X; include Y end | |
puts X.new.x | |
# output: | |
# X | |
# XY |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment