Method accesible only from another class

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.

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

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?

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

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs