Skip to content

Instantly share code, notes, and snippets.

@rkh
Forked from lsegal/snippet.rb
Created March 25, 2010 14:02
Show Gist options
  • Save rkh/343586 to your computer and use it in GitHub Desktop.
Save rkh/343586 to your computer and use it in GitHub Desktop.
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