[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[rcr] TOPLEVEL_OBJECT

Daniel Brumbaugh Keeney

3/26/2008 9:28:00 AM

This is a Ruby Change Request.
http://rcrchive.n...

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 Keeney

2 Answers

Trans

3/26/2008 11:41:00 AM

0



On Mar 26, 5:28 am, "Daniel Brumbaugh Keeney"
<devi.webmas...@gmail.com> wrote:
> This is a Ruby Change Request.http://rcrchive.n...
>
> 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.

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

Arlen Cuss

3/26/2008 12:15:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Wed, Mar 26, 2008 at 10:41 PM, Trans <transfire@gmail.com> 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