Check if module exists in ruby

Viewed 14994

I'm dynamically defining a module name from an argument passed on the cli, for example Required::Module::#{ARGV.first}

Is there any way to check if that module exists? Also, how would I run methods on it not knowing it's exact name?

8 Answers

Get the class if it exists:

dynamic_klass = "Required::Module::#{ARGV.first}".classify.safe_constantize

Call a method on the class if it does:

dynamic_klass.send("some_method") if dynamic_klass.present?
Related