'best way to invoke a class method dynamically in ruby
I have controller "MyController" which has a class method "my_controller_class_method" which accepts arguments a,b,c
is there a way to pass "MyController.my_controller_class_method" as an argument to another method so I could do something like that:
def use_another_method(other_method,a,b,c)
#invoke method inside
other_method(a,b,c)
end
and then I could run
use_another_method(MyController.my_controller_class_method,a1,b1,c1)
Solution 1:[1]
You can use .method to get an object representing that method:
method_object = MyController.method(:my_controller_class_method)
method_object.call(a, b, c)
method_object.(a, b, c) # shorter syntax
That object can then be passed around and used as a function argument, just like any other object.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Grayson |
