Forum: Ruby top-level methods private?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark Volkmann (Guest)
on 2006-06-12 23:01
(Received via mailing list)
I understand that methods defined at the top-level, not in any class,
become methods of the class Object. Pickaxe 2 says in its discussion
of the top-level environment (page 293 in my paper copy) "we're
actually creating (private) instance methods for class Object". I
think the "(private)" part is wrong because I can invoke top-level
methods using a receiver, which I shouldn't be able to do for private
methods.

Here's an example.

def foo; puts 'in foo'; end
class Bar; end
bar = Bar.new
bar.foo

Am I misunderstanding this?
Joel VanderWerf (Guest)
on 2006-06-12 23:24
(Received via mailing list)
Mark Volkmann wrote:
> def foo; puts 'in foo'; end
> class Bar; end
> bar = Bar.new
> bar.foo
>
> Am I misunderstanding this?
>

Don't you get an error when you call bar.foo?

$ ruby -e "
def foo; puts 'in foo'; end
class Bar; end
bar = Bar.new
bar.foo
"
-e:5: private method `foo' called for #<Bar:0xb7dcccec> (NoMethodError)
Mark Volkmann (Guest)
on 2006-06-12 23:34
(Received via mailing list)
On 6/12/06, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> >
> $ ruby -e "
> def foo; puts 'in foo'; end
> class Bar; end
> bar = Bar.new
> bar.foo
> "
> -e:5: private method `foo' called for #<Bar:0xb7dcccec> (NoMethodError)

Hmm ... yes I do. However, I don't get an error if I enter the same
code in irb. That's all I had tried before because I thought it would
be equivalent. I wonder why it's different there.
5cef3d10927d49ddb5975ef74a1db379?d=identicon&s=25 Matthew S. (notmatt)
on 2006-06-12 23:34
(Received via mailing list)
On Jun 12, 2006, at 22:22, Joel VanderWerf wrote:

>>
> $ ruby -e "
> def foo; puts 'in foo'; end
> class Bar; end
> bar = Bar.new
> bar.foo
> "
> -e:5: private method `foo' called for #<Bar:0xb7dcccec>
> (NoMethodError)

Am I right in guessing that Mark is using irb to evaluate this code,
and that Joel is not?  irb has some odd hooks in it, in my experience:

ruby -e "def foo; end; puts Object.private_methods.include?('foo')"
true

irb(main):001:0> def foo; end; puts Object.private_methods.include?
('foo')
false


matthew smillie.
Alexandru Popescu (Guest)
on 2006-06-12 23:37
(Received via mailing list)
Using both 1.8.2 and 1.8.4: no... the method foo is invoked.

./alex
--
.w( the_mindstorm )p.
Alexandru Popescu (Guest)
on 2006-06-12 23:41
(Received via mailing list)
Ohhh... indeed I have tried using irb. Weird!

./alex
--
.w( the_mindstorm )p.
Joel VanderWerf (Guest)
on 2006-06-12 23:47
(Received via mailing list)
Alexandru Popescu wrote:
> Ohhh... indeed I have tried using irb. Weird!

In irb, 'def foo; end' apparently creates a public method of the "main"
object:

irb(main):001:0> self
=> main
irb(main):002:0> def foo; puts "FOO"; end
=> nil
irb(main):003:0> self.foo
FOO
=> nil
irb(main):004:0> class << self; puts
(methods-private_methods).grep(/foo/); end
foo
=> nil

So the "top level" of irb differs from the normal top level of ruby.
Alexandru Popescu (Guest)
on 2006-06-13 00:03
(Received via mailing list)
Not sure I understand:

irb> sefl
=> main

> ruby -e "puts self" => main

why are these different?

./alex
--
.w( the_mindstorm )p.
This topic is locked and can not be replied to.