Access to private methods from the class level

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

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

···

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

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 = 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
=> nil
irb(main):011:0> Kam.call_private
It's private

Hope this helps,

Jesus.

···

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

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.

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

Keep in mind:

access restriction != security

- Josh

···

On May 28, 2009, at 12:39 AM, Jesús Gabriel y Galán wrote:

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