Reputation: 3797
Say I have a class XXXPriceDocument
that may have an inner class LargePackageCharge
, how can I check if it does?
Upvotes: 1
Views: 298
Reputation: 44675
You can do:
XXXPriceDocument.constants.include?(:LargePackageCharge)
or
defined?(XXXPriceDocument::LargePackageCharge)
or
XXXPriceDocument.const_defined?(:LargePackageCharge)
It gets slightly trickier in rails, as constants may not be loaded yet. You will need to get around this with:
class Module
def const_exists?(mod)
!!const_get(mod)
rescue NameError
false
end
end
XXXPriceDocument.const_exists?(:LargePackageCharge)
Upvotes: 6