[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

access to private methods from the class level

Farmer Schlutzenberg

5/28/2009 6:42:00 AM

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.
--
Posted via http://www.ruby-....

4 Answers

Joshua Ballanco

5/28/2009 7:37:00 AM

0

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

Jesús Gabriel y Galán

5/28/2009 7:39:00 AM

0

On Thu, May 28, 2009 at 8:42 AM, Sandworth Meb <farmsal@yahoo.com> wrote:
> 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.

Joshua Ballanco

5/28/2009 7:44:00 AM

0

On May 28, 2009, at 12:39 AM, Jes=FAs Gabriel y Gal=E1n wrote:

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

Keep in mind:

access restriction !=3D security


- Josh=

Daniel DeLorme

5/29/2009 3:10:00 AM

0

Sandworth Meb wrote:
> 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