[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to call private class method?

estebanjang

10/22/2006 7:25:00 AM

Hi,
In the following code, how can I call "Hello.jane" method?

class Hello
def Hello.jane
puts "I am jane"
end
private_class_method :jane
end

All the following generate NoMethodError:

[1] From within class body
class Hello
Hello.jane
end

[2] From another class method
class Hello
def Hello.call_jane
Hello.jane
end
end
Hello.call_jane

[3] From an instance
class Hello
def call_jane2
Hello.jane
end
end
a = Hello.new
a.call_jane2

Is there any context where I can call Hello.jane?
Also, I am using private_class_method, since the "private" keyword
doesn't seem to have any effect on class methods. Am I missing
something?
Thanks,

-Steve

5 Answers

Paul Lutus

10/22/2006 8:46:00 AM

0

estebanjang@gmail.com wrote:

> Hi,
> In the following code, how can I call "Hello.jane" method?
>
> class Hello
> def Hello.jane
> puts "I am jane"
> end
> private_class_method :jane
> end

Code: ***************************

#!/usr/bin/ruby -w

class Hello
def Hello.jane
puts "I am jane"
end
private_class_method :jane

def Hello.access_from_world
jane
end

end

class Howdy < Hello
def Howdy.from_child
jane
end
end

Hello::access_from_world

Howdy::from_child

Output:

I am jane
I am jane

Explanation: **********************

First, because of how it has been declared, your method "Hello.jane" is a
class method, not an instance method. It lives at the class level, so it
must be called using the syntax reserved for class methods.

Second, private methods can only be accessed by other methods of that class
or derived classes, not from outside the class at all. This is by design.

--
Paul Lutus
http://www.ara...

Dirk Lüsebrink

10/22/2006 9:21:00 AM

0

unknown wrote:
> Hi,
> In the following code, how can I call "Hello.jane" method?
>
> class Hello
> def Hello.jane
> puts "I am jane"
> end
> private_class_method :jane
> end


Hello.send :jane

will work, send also works with private methods.

class Hello
def Hello.call_jane
jane
end
end

Hello.call_jane

works for me also.

dirk

--
Posted via http://www.ruby-....

Ross Bamford

10/22/2006 10:46:00 AM

0

On Sun, 2006-10-22 at 18:21 +0900, Dirk Lüsebrink wrote:
> unknown wrote:
> > Hi,
> > In the following code, how can I call "Hello.jane" method?
> >
> > class Hello
> > def Hello.jane
> > puts "I am jane"
> > end
> > private_class_method :jane
> > end
>
>
> Hello.send :jane
>
> will work, send also works with private methods.

I believe that will fail under 1.9 though, where send no longer calls
private methods. IIRC the method you'll need there is 'funcall' instead
of 'send'.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk


Tomasz Wegrzanowski

10/23/2006 12:50:00 AM

0

On 10/22/06, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
> On Sun, 2006-10-22 at 18:21 +0900, Dirk Lüsebrink wrote:
> > Hello.send :jane
> >
> > will work, send also works with private methods.
>
> I believe that will fail under 1.9 though, where send no longer calls
> private methods. IIRC the method you'll need there is 'funcall' instead
> of 'send'.

"Private" messages can only be send to self.
If you want to send such message somewhere else, just self !

Hello.instance_eval { jane }

Within the block self is Hello.

--
Tomasz Wegrzanowski [ http://t-a-w.blo... ]

estebanjang

10/24/2006 6:48:00 PM

0

Cool,
Thanks everyone!

-Steve

Tomasz Wegrzanowski wrote:
> On 10/22/06, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
> > On Sun, 2006-10-22 at 18:21 +0900, Dirk Lüsebrink wrote:
> > > Hello.send :jane
> > >
> > > will work, send also works with private methods.
> >
> > I believe that will fail under 1.9 though, where send no longer calls
> > private methods. IIRC the method you'll need there is 'funcall' instead
> > of 'send'.
>
> "Private" messages can only be send to self.
> If you want to send such message somewhere else, just self !
>
> Hello.instance_eval { jane }
>
> Within the block self is Hello.
>
> --
> Tomasz Wegrzanowski [ http://t-a-w.blo... ]