开发者

Better way to call a class method in ruby?

开发者 https://www.devze.com 2023-02-09 20:37 出处:网络
I need to call a class method in ruby, where I receive the method name as a string, and I already know the class.

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消