I have a utility class defined using the class method class. In the Rails console, when I search for a class using Object.const_defined?, it returns false. But after calling one of the methods of the class or creating an instance of the class, Object.const_defined?returns true. Is it because of some lazy instance loading? Is there any other way to check for a class that will return true, even if I haven't created anything yet?
Object.const_defined?
This is a kind of hack, but it works
Object.const_get(:ClassName).is_a?(Class) rescue false
The above statement will return true if the class is defined and returns false otherwise
safe_constantize.
your_class = "YourClassName".safe_constantize if your_class && your_class.class == Class your_class.new(options).run end
Source: https://habr.com/ru/post/1525687/More articles:How to print a payload for an exception in a message stream mule - muleErrors when starting the Grunt build for the first time - javascriptWhy should the RestTemplate GET response in JSON be in XML? - javaCreate a linq query to search for a contact, just like a smartphone does - c #Scala compiling a value class is not suitable for a base type with a partial function method - scalabar graph of maximum field date dates through unique hosts - elasticsearchType.GetProperties excludes properties using the [] operator - c #"[" and "]" characters messed up get-childitem - directoryWPF - change flag checkbox for image (or hide) - checkboxVisualforce: Uncaught TypeError: unable to read 'tid' property from undefined - javascriptAll Articles