access to private methods from the class level

S

Sandworth Meb

Hi,

I would like to be able to have something like:
A class with a private method "jim" which can be called on objects of
the class, when self is the class.

i.e. like:

class Kam

def self.call_private
kam=Kam.new
kam.private_method
end

private

def private_method
puts "can't do that"
end

end

Kam.call_private #exception: called private method

Since this doesn't work, is something like this possible? Or is this
sort of thing supposed to be bad design? It seems natural enough to me.
 
J

Joshua Ballanco

class Kam

def self.call_private
kam=Kam.new
kam.private_method
end

private

def private_method
puts "can't do that"
end

end

Kam.call_private #exception: called private method

You can't do that because private methods cannot have an explicit
caller (that is if "bar" is a private method, you can't do "foo.bar"
or "self.bar" inside of foo, but you can simply do "bar" inside of
foo). I would also STRONGLY suggest you rethink what you're attempting
to do. If you need to expose a method, then expose it. If you need a
method to only be exposed on the class, then use a class method.

That said (you've been warned!), you can do this:

class Kam
def self.call_private
kam = Kam.new
kam.instance_eval("private_method")
end

private

def private_method
puts "can do this"
end
end

Kam.call_private


...but my STRONG suggestion is that you DON'T do that.

Cheers,

Josh
 
J

Jesús Gabriel y Galán

Hi,

I would like to be able to have something like:
A class with a private method "jim" which can be called on objects of
the class, when self is the class.

i.e. like:

class Kam

=A0def self.call_private
=A0 =A0kam=3DKam.new
=A0 =A0kam.private_method
=A0end

private

=A0def private_method
=A0 =A0puts "can't do that"
=A0end

end

Kam.call_private #exception: called private method

Since this doesn't work, is something like this possible? Or is this
sort of thing supposed to be bad design? It seems natural enough to me.

Here is one way (access restriction is not very strict in Ruby):

irb(main):001:0> class Kam
irb(main):002:1> def self.call_private
irb(main):003:2> k =3D Kam.new
irb(main):004:2> k.send:)private_method)
irb(main):005:2> end
irb(main):006:1> private
irb(main):007:1> def private_method
irb(main):008:2> puts "It's private"
irb(main):009:2> end
irb(main):010:1> end
=3D> nil
irb(main):011:0> Kam.call_private
It's private

Hope this helps,

Jesus.
 
D

Daniel DeLorme

Sandworth said:
Hi,

I would like to be able to have something like:
A class with a private method "jim" which can be called on objects of
the class, when self is the class.
[snip]
Kam.call_private #exception: called private method

Since this doesn't work, is something like this possible? Or is this
sort of thing supposed to be bad design? It seems natural enough to me.

It seems to me like what you're looking for is "protected", not private

Daniel
 

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,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top