2

I have a variable handler whose value can be flight_gds, flight_commission etc.

There are classes in my application which has names like FlightGds, FlightCommission etc. under module FlightManager.

I want to execute the function of class based on the value of the handler.
The function name is same in all classes, only the class name is different which is dependent on the handler.

For example:
If handler name is 'flight_gds', then the function is FlightManager::FlightGds.calculate()

I am trying something like this:

FlightManager::handler.camelize.calculate()

But I am getting undefined method handler error.

Is there any way to do this or I should go with if/else loop?

1 Answer 1

9

You should use constantize to get a class based on it's name in string:

"FlightManager::#{handler.camelize}".constantize.calculate())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.