You’re just in a giant Object.class_eval block

>> 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!

About these ads

2 Comments

Filed under ruby

2 responses to “You’re just in a giant Object.class_eval block

  1. 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]

  2. 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s