# Toplevel\_object

**URL:** <https://www.ruby-forum.com/t/toplevel-object/132971>\
**Category:** Ruby\
**Created:** [March 26, 2008, 10:29am UTC](https://www.ruby-forum.com/t/toplevel-object/132971 "2008-03-26T10:29:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [March 26, 2008, 10:29am UTC](https://www.ruby-forum.com/t/toplevel-object/132971/1 "2008-03-26T10:29:16Z")

</div>

This is a Ruby Change Request.  
[http://rcrchive.net/rcrs/19](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.

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [March 26, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/toplevel-object/132971/2 "2008-03-26T12:42:13Z")

</div>

On Mar 26, 5:28 am, “Daniel Brumbaugh K.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [March 26, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/toplevel-object/132971/3 "2008-03-26T13:18:25Z")

</div>

Hi,

On Wed, Mar 26, 2008 at 10:41 PM, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂 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
