Skip to content

Instantly share code, notes, and snippets.

@lsegal
Created March 25, 2010 06:26
Show Gist options
  • Save lsegal/343253 to your computer and use it in GitHub Desktop.
Save lsegal/343253 to your computer and use it in GitHub Desktop.
class Object
def self.method_added(name)
const_set(:InstanceMethods, Module.new) unless defined?(self::InstanceMethods)
meth = instance_method(name).bind(self.allocate)
self::InstanceMethods.send(:define_method, name, &meth)
remove_method(name)
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