How do I get the class of a BasicObject instance?

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

    I don't know about doing it in Ruby, but this is straightforward using the C API to Ruby. The RubyInline Gem makes adding bits of C to your Ruby code quite easy:

    require 'inline'
    class Example
      inline do |builder|  
        builder.c_raw_singleton < 1
          VALUE true_class(VALUE self, VALUE to_test) {
            return rb_obj_class(to_test);
          }
    SRC
       end
    end
    

    And then:

    1.9.2p180 :033 > Example.true_class(20.minutes)
     => ActiveSupport::Duration 
    

提交回复
热议问题