Toplevel_object

This is a Ruby Change Request.
http://rcrchive.net/rcrs/19

Ruby lacks a TOPLEVEL_OBJECT constant, which provides access to the
object resulting from this Ruby expression:

eval( ‘self’, ::TOPLEVEL_BINDING )

That object is a built-in object of the Ruby programming language,
which should be accessible from any point in Ruby code. It is
well-suited to be assigned to a constant. This constant assignment is
comparable to TOPLEVEL_BINDING, in that, while any library could
assign the value to a constant, it is an object inherent to the Ruby
programming language, and therefore should be assigned to a constant
by the core library.

Specifically, a use case where I found the need to assign
TOPLEVEL_OBJECT was in a tracing program, where it was helpful to
exclude certain objects from being traced. TOPLEVEL_OBJECT needed to
be referenced to be added to the list of exclusions. As previously
stated, it can be trivially assigned to TOPLEVEL_OBJECT, or found
dynamically via it’s definition above, it should be included in the
Ruby core, and it is inappropriate for any other library to assign it.

Daniel Brumbaugh K.

On Mar 26, 5:28 am, “Daniel Brumbaugh K.”
[email protected] wrote:

comparable to TOPLEVEL_BINDING, in that, while any library could
Ruby core, and it is inappropriate for any other library to assign it.
+1

I also would like to ask why Ruby calls this object “main”, but does
not use it in any other way.

$ irb

self
=> main

T.

Hi,

On Wed, Mar 26, 2008 at 10:41 PM, Trans [email protected] wrote:

I also would like to ask why Ruby calls this object “main”, but does
not use it in any other way.

Found it!

static VALUE
main_to_s(VALUE obj)
{
return rb_str_new2(“main”);
}

I jest, of course. :slight_smile: We also see it actually declared/defined this way:

void
Init_top_self()
{
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);

}

I guess, it needs to be called something - or should be called
something,
just to distinguish it! So here’s a way.

Cheers,
Arlen