method accesible only from another class

M

Mario Ruiz

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.
 
S

Stefano Crocco

Alle gioved=C3=AC 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 (everyon=
e=20
can call it), protected (it can be called only from instances of the same=20
class) or private (can only be called by the implicit receiver, self). What=
=20
you can do is use send, which allows to call every kind of method. In your=
=20
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=
,=20
and there's nothing you can do about it (well, you could override Nueva#sen=
d=20
and Nueva#__send__, if you really need to be sure other classes can't do=20
that, but I don't think it's truly necessary).

I hope this helps

Stefano
 
M

Mario Ruiz

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?
 
S

Stefano Crocco

Alle gioved=C3=AC 13 dicembre 2007, Mario Ruiz 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=
=20
private module method (or private class method) is a method which can only =
be=20
called with the module as receiver. For example:

module M
=20
def self.a_method
puts "a_method"
end
private_class_method :a_method

end

M.a_method
=3D> NoMethodError: private method `a_method' called for M:Module.

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

Stefano
 
M

Mario Ruiz

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

Mario Ruiz

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top