Created
December 4, 2008 09:53
-
-
Save godfat/31903 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
# 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