Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails how do I check for existance of a class when not instantiated yet?

I have a utility class defined with a bunch of class methods. In the Rails console, when I search for the class using Object.const_defined? it returns false. But after calling one of the class methods or creating an instance of the class, Object.const_defined? returns true. Is this because of some kind of lazy loading instantiation kind of thing? Is there another way I can check for the existence of a class which will return true, even if I haven't instantiated anything yet?

like image 304
zako42 Avatar asked Dec 12 '22 07:12

zako42


1 Answers

This is kind of a hack, but it works

Object.const_get(:ClassName).is_a?(Class) rescue false

Above statement will return true if the class is defined and return false otherwise

like image 102
usha Avatar answered Apr 09 '23 06:04

usha