# Method accesible only from another class

**URL:** <https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013>\
**Category:** Ruby\
**Created:** [December 13, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013 "2007-12-13T12:29:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [December 13, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013/1 "2007-12-13T12:29:41Z")

</div>

I need that a method can be accesible only from other class but only  
this method:

class Nueva  
def metodo  
puts ‘metodo’  
end  
def metodo3  
puts ‘metodo3’  
end  
private  
def metodo2  
puts ‘metodo2’  
end  
end

class Nueva2  
def metodo  
puts ‘Nueva2metodo’  
Nueva.new.metodo2()  
end  
def metodo2  
puts ‘Nueva2metodo2’  
end  
end

I need to call the Nueva.metodo2 from Nueva2.metodo and I want this  
method can be accesible only from Nueva2 class. Also I don’t want the  
other methods from Nueva Class.

How can I do it???

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [December 13, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013/2 "2007-12-13T13:07:16Z")

</div>

Alle giovedÃ¬ 13 dicembre 2007, Mario R. ha scritto:

> private  
> def metodo2  
> Thank you in advance.  
> I don’t think you can do exactly that. A method can only be public  
> (everyone  
> can call it), protected (it can be called only from instances of the  
> same  
> class) or private (can only be called by the implicit receiver, self).  
> What  
> you can do is use send, which allows to call every kind of method. In  
> your  
> case:

class Nueva2  
def metodo  
puts ‘Nueva2metodo’  
Nueva.new.send(:metodo2)  
end  
def metodo2  
puts ‘Nueva2metodo2’  
end  
end

Note that also other classes can access the Nueva#metodo2 method using  
send,  
and there’s nothing you can do about it (well, you could override  
Nueva#send  
and Nueva# **send** , if you really need to be sure other classes can’t do  
that, but I don’t think it’s truly necessary).

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [December 13, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013/3 "2007-12-13T13:07:54Z")

</div>

Maybe a solution can be if I write the classes inside a module and I can  
declare a method as private for this module, is it possible?

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [December 13, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013/4 "2007-12-13T14:10:47Z")

</div>

I like very much the send option, and it works really well.  
I am only thinking in more possiblities.  
Thanks a lot. 🙂

Stefano C. wrote:

> Alle giovedÃ¬ 13 dicembre 2007, Mario R. ha scritto:
> 
> > Maybe a solution can be if I write the classes inside a module and I can  
> > declare a method as private for this module, is it possible?
> 
> No. There are private module methods, but they’re another thing  
> enterely. A  
> private module method (or private class method) is a method which can  
> only be  
> called with the module as receiver. For example:
> 
> module M
> 
> def self.a\_method  
> puts “a\_method”  
> end  
> private\_class\_method :a\_method
> 
> end
> 
> M.a\_method  
> =\> NoMethodError: private method `a\_method’ called for M:Module.
> 
> Why don’t you like to use send as I suggested?
> 
> Stefano

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [December 13, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013/5 "2007-12-13T13:23:47Z")

</div>

Alle giovedÃ¬ 13 dicembre 2007, Mario R. ha scritto:

> Maybe a solution can be if I write the classes inside a module and I can  
> declare a method as private for this module, is it possible?

No. There are private module methods, but they’re another thing  
enterely. A  
private module method (or private class method) is a method which can  
only be  
called with the module as receiver. For example:

module M

def self.a\_method  
puts “a\_method”  
end  
private\_class\_method :a\_method

end

M.a\_method  
=\> NoMethodError: private method `a\_method’ called for M:Module.

Why don’t you like to use send as I suggested?

Stefano

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [December 13, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/method-accesible-only-from-another-class/123013/6 "2007-12-13T14:13:52Z")

</div>

About the solution with the module… could I use in this method a  
variable of the class Nueva?
