Skip to content

Instantly share code, notes, and snippets.

@godfat
Created Dec 4, 2008
Embed
What would you like to do?
# http://rubinius.lighthouseapp.com/projects/5089/tickets/614-calling-a-class-method-on-an-object-s-metaclass-should-invoke-the-class-method-of-the-object-s-class#ticket-614-5
class Object
# The hidden singleton lurks behind everyone
def metaclass; class << self; self; end; end
def meta_eval(&blk)
metaclass.instance_eval(&blk)
end
# Adds methods to a metaclass
def meta_def name, &blk
meta_eval { define_method name, &blk }
end
end
class B
end
b = B.new
# false in Rubinius, true in MRI, JRuby
p b.metaclass.superclass.object_id == B.metaclass.object_id
b.meta_def(:cheese) {'cheshire'}
# false in all implementation
p b.metaclass.superclass.object_id == B.metaclass.object_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment