Better way to call a class method in ruby?
I need to call a class method in ruby, where I receive the method name as a string, and I already know the class.
Is there a better way than this?
开发者_运维百科(X.method method_name.to_sym).call
(X.method method_with_params_name.to_sym).call(param1, param2)
You can use send
which is at least a bit cleaner and more common.
TheClass.send(method_name)
TheClass.send(method_name, param1, param2)
精彩评论