开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜