Interesting behaviour

D

Deepak Gole

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

When I does following thing on irb console

def foo
1
end

foo ===>o/p 1
foo.foo ===>o/p 1
foo.foo.foo ===>o/p 1
foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo............foo
===>o/p 1

I got above o/p


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

But when I done same thing in Scite editor. I got error

def foo
1
end

p foo
p foo.foo =>o/p private method `foo' called for 1:Fixnum (NoMethodError)


How ??


thanks
DG
 
B

Brian Candler

Deepak said:
def foo
1
end

foo ===>o/p 1
foo.foo ===>o/p 1
foo.foo.foo ===>o/p 1
foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo............foo
===>o/p 1

I got above o/p

I see the same with ruby 1.8.6.

It looks like you have defined foo as a method in Object, and hence is
available to all objects:
=> ["foo"]

Ruby 1.9 makes this slightly better by making the method private:

$ irb19 --simple-promptNoMethodError: undefined method `foo' for "hello":String
from (irb):1
NoMethodError: private method `foo' called for "hello":String
from (irb):3
=> [:foo]

I'm not sure why irb doesn't define methods as singleton methods of the
'main' (top level) object. You can do this yourself with a bit of
fiddling:
NoMethodError: undefined method `bar' for 1:Fixnum
from (irb):4
from /usr/local/bin/irb19:12:in `<main>'
 
P

Pascal J. Bourguignon

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

When I does following thing on irb console

def foo
1
end

foo ===>o/p 1
foo.foo ===>o/p 1
foo.foo.foo ===>o/p 1
foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo.foo............foo
===>o/p 1

I got above o/p


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

But when I done same thing in Scite editor. I got error

def foo
1
end

p foo
p foo.foo =>o/p private method `foo' called for 1:Fixnum (NoMethodError)


How ??

Why? Ask youself why foo.foo.foo --> 1 in the irb console!

Try: self.class
in both Scite and irb...
 
B

Brian Candler

Pascal said:
Why? Ask youself why foo.foo.foo --> 1 in the irb console!

Try: self.class
in both Scite and irb...

It's not quite as simple as just looking at 'self', and it took me a
long time to find out why.

Ruby has a concept of a 'the current object' which is exposed using
'self', but there is also a more hidden concept of 'the current class'
where method definitions go. I don't think there's an easy way to see
the current class, but you can set it using class or class_eval.
Foo
=> nilbar
=> nilNoMethodError: undefined method `bar' for #<Foo:0xb7c89710>
from (irb):5
from :0

But:
Foo
=> nilNoMethodError: undefined method `baz' for Foo:Class
from (irb):8
from :0baz
=> nil

So in both cases, self is the class Foo, but in the first case methods
are defined in the singleton class of Foo, whilst in the second case
they are defined as instance methods in the class Foo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top