[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

toplevel is all levels ?

T. Onoma

10/29/2004 3:15:00 AM

My, my. I'm always discovering something new about Ruby. Strange how POLS can
also mean POMS in a good way ;)

I just discovered that a method defined at the toplevel is accessable _inside_
my objects.

irb(main):001:0> def t
irb(main):002:1> puts "c"
irb(main):003:1> end
=> nil
irb(main):004:0> class R
irb(main):005:1> def f
irb(main):006:2> t
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> r = R.new
=> #<R:0x4032bdc0>
irb(main):010:0> r.t
c
=> nil
irb(main):011:0> r.f
c

Maybe I look foolish for not knowing this, but I really had no idea. When I
wanted this behavior before I would always put the def in module Kernel or
class Object.

How is #t getting inside r ? Does Toplevel == Object ? Guess I was thinking
that Toplevel was its own space --a subclass of Object.

T.


3 Answers

Yukihiro Matsumoto

10/29/2004 3:19:00 AM

0

Hi,

In message "Re: toplevel is all levels ?"
on Fri, 29 Oct 2004 12:14:48 +0900, "trans. (T. Onoma)" <transami@runbox.com> writes:

|How is #t getting inside r ? Does Toplevel == Object ? Guess I was thinking
|that Toplevel was its own space --a subclass of Object.

def statement at the top level defines private method in Object
class.

matz.


T. Onoma

10/29/2004 5:27:00 AM

0

On Thursday 28 October 2004 11:19 pm, Yukihiro Matsumoto wrote:
| Hi,
|
| In message "Re: toplevel is all levels ?"
|
| on Fri, 29 Oct 2004 12:14:48 +0900, "trans. (T. Onoma)"
<transami@runbox.com> writes:
| |How is #t getting inside r ? Does Toplevel == Object ? Guess I was
| | thinking that Toplevel was its own space --a subclass of Object.
|
| def statement at the top level defines private method in Object
| class.

I see. Thank you. Interestingly irb doesn't throw the error.

T.


Bill Atkins

10/29/2004 12:10:00 PM

0

Why should it? If t is a private method of Object, and R is an
object, then there's no reason for it not to work.

Bill

On Fri, 29 Oct 2004 14:27:17 +0900, trans. (T. Onoma)
<transami@runbox.com> wrote:
> On Thursday 28 October 2004 11:19 pm, Yukihiro Matsumoto wrote:
> | Hi,
>
>
> |
> | In message "Re: toplevel is all levels ?"
> |
> | on Fri, 29 Oct 2004 12:14:48 +0900, "trans. (T. Onoma)"
> <transami@runbox.com> writes:
> | |How is #t getting inside r ? Does Toplevel == Object ? Guess I was
> | | thinking that Toplevel was its own space --a subclass of Object.
> |
> | def statement at the top level defines private method in Object
> | class.
>
> I see. Thank you. Interestingly irb doesn't throw the error.
>
> T.
>
>