How to call private class method?

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

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.arachnoid.com

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-forum.com/\.

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'.

···

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.

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

"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.

···

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'.

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

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.blogspot.com/ ]