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?
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?
Check for module existence using the const_get
method:
begin
mod = Required::Module::const_get "ModuleName"
#It exists
rescue NameError
#Doesn't exist
end
defined?(Required::Module)
gives "constant"
if it exists, and nil
if it doesn't.
Update: Sorry, didn't read your question properly.
defined?(eval("Required::Module::"+string))
should give you what you're after.
Use const_defined?
for this.
Required::Module.const_defined?(:ModuleName)
returns true or false.