Better way to dynamically call namespaced methods

86 views Asked by At

I was curious when building a method to call an endpoint using the Net::HTTP library in rails. As the endpoint I was calling accepted the same body but with multiple request methods, I wanted to dynamically call Net::HTTP.

To do this, I used this concoction

req_method = Object.const_get "Net::HTTP::#{method.capitalize}"

But feel this is ugly and not elegant. Is there a more rails or just a cleaner way to call that method in a dynamic way?

1

There are 1 answers

3
valduane On

Why you can't just do something like this? Net::HTTP.public_send(method.capitalize)