Joshua Ballanco
5/28/2009 7:37:00 AM
On May 27, 2009, at 11:42 PM, Sandworth Meb wrote:
> 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