# Calling private method in ruby

**URL:** <https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132>\
**Category:** Ruby\
**Created:** [June 29, 2007, 8:31am UTC](https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132 "2007-06-29T08:31:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [June 29, 2007, 8:31am UTC](https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132/1 "2007-06-29T08:31:14Z")

</div>

Hi there,

I have a little problem regarding private methods in Ruby.  
I have the following case:

class MyClass  
private  
def my\_method  
puts “something from my method”  
end  
end

myClass = MyClass.new  
begin  
myClass.my\_method  
rescue  
puts “Method cannot be called because is private”  
end

myClass.send(“my\_method”)

So, can anyone help me by explaining in a logical way, what is the point  
of private methods in ruby since I can call it using “send” ?

Thanks in advance,

Alin

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [June 29, 2007, 9:11am UTC](https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132/2 "2007-06-29T09:11:44Z")

</div>

On Jun 29, 2:31 am, Alin P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> So, can anyone help me by explaining in a logical way, what is the point  
> of private methods in ruby since I can call it using “send” ?
> 
> Thanks in advance,
> 
> Alin
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Maybe the fact that you have to use send will at least make you  
appreciate that the method IS private in a real world scenario. But to  
be honest, I think that making methods private is usually unnecessary  
to begin with, but it does help with something I’ve forgotten about.  
And because you’re getting at the method through another method, it’ll  
take longer to act on it; not excruciatingly longer, though.

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [June 29, 2007, 9:24am UTC](https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132/3 "2007-06-29T09:24:46Z")

</div>

Alle venerdÃ¬ 29 giugno 2007, Alin P. ha scritto:

> end  
> So, can anyone help me by explaining in a logical way, what is the point  
> of private methods in ruby since I can call it using “send” ?
> 
> Thanks in advance,
> 
> Alin

In general, the idea behind private methods is that they can’t be called  
from  
outside the object they belong to. In ruby, this is achieved by allowing  
calling them only without an explicit receiver. This means that the you  
can  
call them when the receiver of the method is the implicit receiver  
(self),  
that is, only from within instance methods:

class MyClass

def private\_method  
puts “This is private\_method”  
end  
private :private\_method

def test\_method  
puts “This test method is abouto to call private\_method”  
private\_method  
end

end

obj = MyClass.new  
obj.test\_method  
puts “—”  
obj.private\_method

## The output is: This test method is abouto to call private\_method This is a private method

./script.rb:20: private method `private\_method’ called for  
#MyClass:0xb7c171d8 (NoMethodError)

Note that no receiver is allowed for private method, not even self. So,  
test\_method couldn’t have been written as:

def test\_method  
puts “This test method is abouto to call private\_method”  
self.private\_method  
end

Doing this would again result in a NoMethodError exception.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [June 29, 2007, 9:33am UTC](https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132/4 "2007-06-29T09:33:27Z")

</div>

Thanks Stefano,

But my issue is not “what is a private method”.  
I’m curios about “send” method, why there is a workaround to get to  
private methods from OUTSIDE when this shouldn’t happen ?

Alin

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [June 29, 2007, 9:51am UTC](https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132/5 "2007-06-29T09:51:48Z")

</div>

Alin P. wrote:

> Thanks Stefano,
> 
> But my issue is not “what is a private method”.  
> I’m curios about “send” method, why there is a workaround to get to  
> private methods from OUTSIDE when this shouldn’t happen ?

You can also do obj.instance\_eval{ privmethod() }

This is because ruby is a language that ALLOWS you to do things, not  
RESTRICT you from doing things. It’s a matter of philosophy.

Daniel

---

<div class="post-metadata">

**Author:** ![chucky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@chucky](https://www.ruby-forum.com/u/chucky)\
**Post date:** [June 29, 2007, 9:57am UTC](https://www.ruby-forum.com/t/calling-private-method-in-ruby/108132/6 "2007-06-29T09:57:43Z")

</div>

Daniel DeLorme wrote:

> Alin P. wrote:
> 
> > Thanks Stefano,
> > 
> > But my issue is not “what is a private method”.  
> > I’m curios about “send” method, why there is a workaround to get to  
> > private methods from OUTSIDE when this shouldn’t happen ?
> 
> You can also do obj.instance\_eval{ privmethod() }
> 
> This is because ruby is a language that ALLOWS you to do things, not  
> RESTRICT you from doing things. It’s a matter of philosophy.
> 
> Daniel

Thank you Daniel,

I think you cleared my problem.

Alin
