> Programming Languages > Ruby
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Ruby



Ruby - "About top-level method" in Programming Languages


Old 06-21-2004   #1
..m ..ngsh.. ....
 
Default About top-level method

Hello!

I'm reading a book named "Teach yourself Ruby".
In chapter 2, it says that top-level methods are Object's private instance
methods.
But my test shows different results.

In irb, I tested it like the following.

def my_f
p "my_f"
end

Object.my_f #=> print "my_f"
obj = Object.new
obj.my_f #=> print "my_f"

Then, my_f is an instance method, and a cl*** method as well?

Can anyone help me understand it?

kong


 
Old 06-21-2004   #2
.... ..nderWe..
 
Default Re: About top-level method

Sam Sungshik Kong wrote:
> Hello!
>
> I'm reading a book named "Teach yourself Ruby".
> In chapter 2, it says that top-level methods are Object's private instance
> methods.
> But my test shows different results.
>
> In irb, I tested it like the following.
>
> def my_f
> p "my_f"
> end
>
> Object.my_f #=> print "my_f"
> obj = Object.new
> obj.my_f #=> print "my_f"
>
> Then, my_f is an instance method, and a cl*** method as well?


Only in irb, as I understand. Try it using 'ruby file.rb', and you'll
get the private method exception. Irb is not quite ruby


 
Old 06-21-2004   #3
..li.. ..ndr..
 
Default Re: About top-level method

> In irb, I tested it like the following.
>
> def my_f
> p "my_f"
> end
>
> Object.my_f #=> print "my_f"
> obj = Object.new
> obj.my_f #=> print "my_f"


Well, you tested it in irb.
Consider:

julian@tenacity test $ cat >> priv-test.rb
def my_f
p "my_f"
end

Object.my_f
obj = Object.new
obj.my_f
julian@tenacity test $ ruby -w priv-test.rb
priv-test.rb:5: private method `my_f' called for Object:Cl*** (NoMethodError)


 
Old 06-21-2004   #4
..brie.. ..n..
 
Default Re: About top-level method

il Mon, 21 Jun 2004 05:32:06 GMT, "Sam Sungshik Kong"
<ssk@chol.nospam.net> ha scritto::


>Then, my_f is an instance method, and a cl*** method as well?
>
>Can anyone help me understand it?
>


well, you ahould consider that:

>> Object.is_a? Object

=> true

everything is_a object including Object as well.
 
Old 06-21-2004   #5
.... ..De..
 
Default Re: About top-level method

On Sunday 20 June 2004 22:33, Sam Sungshik Kong wrote:
> Hello!
>
> I'm reading a book named "Teach yourself Ruby".
> In chapter 2, it says that top-level methods are Object's private instance
> methods.
> But my test shows different results.
>
> In irb, I tested it like the following.
>
> def my_f
> p "my_f"
> end
>
> Object.my_f #=> print "my_f"
> obj = Object.new
> obj.my_f #=> print "my_f"
>
> Then, my_f is an instance method, and a cl*** method as well?
>
> Can anyone help me understand it?


The Object cl*** is the base of every other cl***, and is itself an instance
of cl*** Cl***.

Sean O'Dell


 
Old 06-21-2004   #6
..uric.. ..rnánd..
 
Default Re: About top-level method

On Tue, Jun 22, 2004 at 12:24:21AM +0900, Sean O'Dell wrote:
> > In irb, I tested it like the following.
> >
> > def my_f
> > p "my_f"
> > end
> >
> > Object.my_f #=> print "my_f"
> > obj = Object.new
> > obj.my_f #=> print "my_f"
> >
> > Then, my_f is an instance method, and a cl*** method as well?
> >
> > Can anyone help me understand it?

>
> The Object cl*** is the base of every other cl***, and is itself an instance
> of cl*** Cl***.


irb can sometimes mislead you:

$ ruby
def foo; 1 end
Object.foo
-:2: private method `foo' called for Object:Cl*** (NoMethodError)

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

(It is an old Debian tradition to leave at least twice a year ...)
-- Sven Rudolph


 
Old 06-21-2004   #7
..m ..ngsh.. ....
 
Default Re: About top-level method

Hi, Sean!

Thank you for the reply.
You helped me understand half of the issue.
As Object is a supercl*** of all cl***es (except for Object),
if I add a method to Object, the metacl*** of Object will have it too.
That enlightened me a lot.

However, there's one more thing to solve.
When I define a method at top-level,
it's Object's private instance method.
So it can't be used like receiver.method.
obj.my_f #-> Error because my_f is private.
Object.my_f #->Error because my_f is private.

The question is why irb ignores the error?
Is it intentional or a bug?

I also appreciate all other replies.

kong

"Sean O'Dell" <sean@celsoft.com> wrote in message
news:200406210824.11465.sean@celsoft.com...
> On Sunday 20 June 2004 22:33, Sam Sungshik Kong wrote:
> > Hello!
> >
> > I'm reading a book named "Teach yourself Ruby".
> > In chapter 2, it says that top-level methods are Object's private

instance
> > methods.
> > But my test shows different results.
> >
> > In irb, I tested it like the following.
> >
> > def my_f
> > p "my_f"
> > end
> >
> > Object.my_f #=> print "my_f"
> > obj = Object.new
> > obj.my_f #=> print "my_f"
> >
> > Then, my_f is an instance method, and a cl*** method as well?
> >
> > Can anyone help me understand it?

>
> The Object cl*** is the base of every other cl***, and is itself an

instance
> of cl*** Cl***.
>
> Sean O'Dell
>
>
>



 
Old 06-21-2004   #8
.... ..De..
 
Default Re: About top-level method

On Monday 21 June 2004 10:23, Sam Sungshik Kong wrote:
> Hi, Sean!
>
> Thank you for the reply.
> You helped me understand half of the issue.
> As Object is a supercl*** of all cl***es (except for Object),
> if I add a method to Object, the metacl*** of Object will have it too.
> That enlightened me a lot.
>
> However, there's one more thing to solve.
> When I define a method at top-level,
> it's Object's private instance method.
> So it can't be used like receiver.method.
> obj.my_f #-> Error because my_f is private.
> Object.my_f #->Error because my_f is private.
>
> The question is why irb ignores the error?
> Is it intentional or a bug?
>
> I also appreciate all other replies.


My guess is irb ignores the private flag to allow you to test what you wish
from irb. It's really just a debugging environment, so it makes sense to
disable private flags.

Sean O'Dell


 
Old 06-21-2004   #9
..nn.. ..y-Reynol..
 
Default Re: About top-level method

I don't think irb is ignoring the "private" flag -- you're actually
inside the private scope of an Object instance whenever you're
inputting data at the prompt. That means methods are defined on the
current cl***; i.e., Object. It's easy to test:

(at irb prompt

def test
puts self.id
end

test
=> -542341874 (or similar)

puts self.id
=> -542341874 (or similar)

To show that all 'private' flags are not being ignored, try the
following (again, in irb):

cl*** Foo
private
def test
puts "testing"
end
end

f = Foo.new
f.test
=> NoMethodError: private method `foo' called for #<A:0xbf50fcf4>
from (irb):16

Hope that helps,

Lennon


 
Old 06-21-2004   #10
..m ..ngsh.. ....
 
Default Re: About top-level method

Hi, Lennon!

If I understand correctly, one can't call a private method via object name
(receiver).
Private methods can be called only with self.

So the following code will cause an error (at the top level).

def foo #->this is a private instance method of Object
...
end

obj = Object.new

obj.foo #->Error, but in irb, it's ok.

Best explanation so far is that irb makes an exception for debugging
purpose.


While trying to find the answer, I met more confusing statements in the
Programming Ruby (Cl***es and Objects chapter).

<snip>
At the top level, we're executing code in the context of some predefined
object. When we define methods, we're actually creating (private) singleton
methods for this object. Instance variables belong to this object. And
because we're in the context of Object, we can use all of Object's methods
(including those mixed-in from Kernel) in function form. This explains why
we can call Kernel methods such as puts at the top level (and indeed
throughout Ruby): these methods are part of every object.
</snip>

Is the above explanation correct?
Do I create a singleton method if I define a method at the top level?

I'm getting more and more confused...
Before I started Ruby, I thought I understood OOP concepts very clearly
(with Java, C#, even Python).
But Ruby puts me in a deep confusion with singleton methods, somewhat
different concept of private/protected accessibility, metacl***, etc...
But I like Ruby very much.
I'm sure that if I overcome the difficulties, I will be a better
programmer...

kong


"Lennon Day-Reynolds" <rcoder@gmail.com> wrote in message
news:5d4c612404062110583ed53e46@mail.gmail.com...
> I don't think irb is ignoring the "private" flag -- you're actually
> inside the private scope of an Object instance whenever you're
> inputting data at the prompt. That means methods are defined on the
> current cl***; i.e., Object. It's easy to test:
>
> (at irb prompt
>
> def test
> puts self.id
> end
>
> test
> => -542341874 (or similar)
>
> puts self.id
> => -542341874 (or similar)
>
> To show that all 'private' flags are not being ignored, try the
> following (again, in irb):
>
> cl*** Foo
> private
> def test
> puts "testing"
> end
> end
>
> f = Foo.new
> f.test
> => NoMethodError: private method `foo' called for #<A:0xbf50fcf4>
> from (irb):16
>
> Hope that helps,
>
> Lennon
>
>
>



 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0