I have a module that contains a class called String (among others.) I need to search for a class by name and gracefully back out if there is no such class.
module Mod1 module String end end Mod1.const_get 'String'
still so good. I expect to get a NameError when I try to find a nonexistent class, and that's fine. The problem is that if there is a class with the given name that exists in the global namespace, it returns:
Mod1.const_get 'Fixnum'
I understand the reasons, but my question is: is there a ready-made method for finding a constant in a given namespace?
Now I check the result with
result.to_s.start_with?(namespace)
but this is definitely not the most convenient way to narrow your search.
source share