From: Stefano Crocco Date: 2007-12-13T21:03:14+09:00 Subject: Re: method accesible only from another class Alle giovedì 13 dicembre 2007, Mario Ruiz ha scritto: > 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. 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