How to call private class method?

E

estebanjang

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
 
D

Dirk Lüsebrink

unknown said:
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
 
R

Ross Bamford

=20
=20
Hello.send :jane
=20
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'.

--=20
Ross Bamford - (e-mail address removed)
 
T

Tomasz Wegrzanowski

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.

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

estebanjang

Cool,
Thanks everyone!

-Steve

Tomasz said:
"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.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top