Forum: Ruby access to private methods from the class level

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sandworth M. (Guest)
on 2009-05-28 10:42
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.
Joshua B. (Guest)
on 2009-05-28 11:38
(Received via mailing list)
On May 27, 2009, at 11:42 PM, Sandworth M. wrote:

>    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 (Guest)
on 2009-05-28 11:40
(Received via mailing list)
On Thu, May 28, 2009 at 8:42 AM, Sandworth M. 
<removed_email_address@domain.invalid>
wrote:
>  def self.call_private
> 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 = 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.
Joshua B. (Guest)
on 2009-05-28 11:48
(Received via mailing list)
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):

Keep in mind:

access restriction != security


- Josh
Daniel DeLorme (Guest)
on 2009-05-29 07:11
(Received via mailing list)
Sandworth M. 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
This topic is locked and can not be replied to.