gist: 31903 Download_button fork
public
Public Clone URL: git://gist.github.com/31903.git
Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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
 

Owner

godfat

Revisions