# Access to private methods from the class level

**URL:** <https://www.ruby-forum.com/t/access-to-private-methods-from-the-class-level/168991>\
**Category:** Ruby\
**Created:** [May 28, 2009, 8:42am UTC](https://www.ruby-forum.com/t/access-to-private-methods-from-the-class-level/168991 "2009-05-28T08:42:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [May 28, 2009, 8:42am UTC](https://www.ruby-forum.com/t/access-to-private-methods-from-the-class-level/168991/1 "2009-05-28T08:42:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [May 28, 2009, 9:38am UTC](https://www.ruby-forum.com/t/access-to-private-methods-from-the-class-level/168991/2 "2009-05-28T09:38:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [May 28, 2009, 9:40am UTC](https://www.ruby-forum.com/t/access-to-private-methods-from-the-class-level/168991/3 "2009-05-28T09:40:00Z")

</div>

On Thu, May 28, 2009 at 8:42 AM, Sandworth M. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [May 28, 2009, 9:48am UTC](https://www.ruby-forum.com/t/access-to-private-methods-from-the-class-level/168991/4 "2009-05-28T09:48:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![farmerss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@farmerss](https://www.ruby-forum.com/u/farmerss)\
**Post date:** [May 29, 2009, 5:11am UTC](https://www.ruby-forum.com/t/access-to-private-methods-from-the-class-level/168991/5 "2009-05-29T05:11:01Z")

</div>

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
