August 8, 2008...9:02 am

You’re just in a giant Object.class_eval block

Jump to Comments

>> Object.methods.size
=> 85
>> def i_is_in_ur_Object; "kthxbai" end
=> nil
>> Object.methods.size
=> 86
>> String.new.i_is_in_ur_Object
=> "kthxbai"

Careful what you put in there!

2 Comments

  • Actually, you’re in Kernel, which gets included in Object:

    irb(main):001:0> Kernel.methods.size
    => 147
    irb(main):002:0> def i_is_in_ur_Object; “kthxbai” end
    => nil
    irb(main):003:0> Kernel.methods.size
    => 148
    irb(main):004:0> Object.included_modules
    => [Wirble::Shortcuts, PP::ObjectMixin, Kernel]

  • no, that’ because Kernel also inherit from Object

    >> class Poop; end
    >> def i_is_in_ur_Object; “kthxbai” end
    >> Poop.method_defined?(:i_is_in_ur_Object)
    => true

    You can see that you really are in an instance of Object:

    >> self.class
    => Object

    And in YARV’s code in vm.c around line 1884:

    void
    Init_top_self(void)
    {
    rb_vm_t *vm = GET_VM();

    vm->top_self = rb_obj_alloc(rb_cObject);
    rb_define_singleton_method(rb_vm_top_self(), “to_s”, main_to_s, 0);
    }

    vm->top_self is indeed an instance of Object and to_s rly outputs “main”


Leave a Reply