Re: Differences between irb and ruby.
by Ezra Zygmuntowicz other posts by this author
Jun 30 2006 9:30PM messages near this date
Re: Differences between irb and ruby.
|
Re: Differences between irb and ruby.
Hi-
On Jun 30, 2006, at 9:19 PM, Minkoo Seo wrote:
> Hi Ezra.
>
> You've got it wrong. The point is that though a method that is defined
> outside of any class becomes automatically part of Object, it can't be
> called with exact receiver because the method is *private*.
>
> In other words,
>
> # foo is private method of Object
> def foo
> ..
> end
>
> class Bar; end
>
> # This is okay, because I'm calling self.foo and we do not
> # need to state self as receiver.
> foo
>
> # This is completely wrong. Private method can't be called
> # with exact receiver.
> Bar.new.foo
>
> Hope this helps.
>
> Sincerely,
> Minkoo Seo
>
My irb disagrees with you
irb(main):012:0> def foo
irb(main):013:1> puts 'foo'
irb(main):014:1> end
=> nil
irb(main):015:0> class Bar
irb(main):016:1> end
=> nil
irb(main):017:0> Bar.new.foo
foo
=> nil
irb(main):018:0> Object.private_methods.grep /foo/
=> []
irb(main):019:0> Object.instance_methods.grep /foo/
=> ["foo"]
irb(main):020:0> Bar.instance_methods.grep /foo/
=> ["foo"]
irb(main):021:0> Bar.private_methods.grep /foo/
=> []
But I understand that in a normal ruby script methods def'ed outside
of a class definition becore private methods of Object but its not
the case in irb.
-Ezra
Thread:
Minkoo Seo
Ezra Zygmuntowicz
Minkoo Seo
Ezra Zygmuntowicz
Austin Ziegler
Minkoo Seo
Logan Capaldo
Mauricio Fernandez
|