方法可变

时间:2015-11-24 16:45:12

标签: ruby

我有这样的事情:

case method
when :get    
   HTTParty.get(configuration.endpoint)
when :post
   HTTParty.post(configuration.endpoint)
when :put
   HTTParty.put(configuration.endpoint)
end

怎么做?

HTTParty.{method}(configuration.endpoint)

1 个答案:

答案 0 :(得分:0)

您可以使用public_send ruby​​方法

HTTParty.public_send(method)

或带参数

HTTParty.public_send(method, arg1, arg2)

public_send方法here

的文档