How do I get the class of a BasicObject instance?

后端 未结 8 2035
小鲜肉
小鲜肉 2020-12-08 03:03

I have a script that iterates using ObjectSpace#each_object with no args. Then it prints how many instances exist for each class.

I realized that some

相关标签:
8条回答
  • 2020-12-08 03:39

    For the similar situation where you simply want a class you created that inherits from BasicObject to support the #class method, you can copy the method over from Kernel.

    class Foo < BasicObject
      define_method(:class, ::Kernel.instance_method(:class))
    end
    
    f = Foo.new
    puts f.class
    => Foo
    
    0 讨论(0)
  • 2020-12-08 03:40
    (class << object; self; end).superclass
    
    0 讨论(0)
提交回复
热议问题