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.
I see. Thank you. Interestingly irb doesn't throw the error.
T.
路路路
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.
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.