Skip to content

Instantly share code, notes, and snippets.

@godfat
Created December 4, 2008 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godfat/31903 to your computer and use it in GitHub Desktop.
Save godfat/31903 to your computer and use it in GitHub Desktop.
# 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