I have a class with several methods:
class Test
def initialize (age, height)
@age = age
@height = height
end
def older
@age = @age + 2
end
def shorter
@height = @height - 5
end
end
man = Test.new(40, 170)
man.older
man.shorter
[...]
I want to pass onto object man a custom method, that is, I want to write something like man.variablemethod and set .variablemethod to either .older or .shorter, depending on some other factors. How can I do that?
I figured out that I can call “if condition then man.older“, but I do not want to use if, especially when I have twenty different methods to choose from.
Sounds like you need
send:You can pass a string or symbol representing the name of the method you wish to call, even pass additional arguments too:
This also allows you to call private and protected methods from anywhere: