How do I get the class of a BasicObject instance?

后端 未结 8 2082
小鲜肉
小鲜肉 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
    

提交回复
热议问题