Rails 4.2.0 / Ruby 2.2.1
I am facing inconsistent behaviour when using defined?
to check if a class having given name exists or not. I want to avoid rescuing exceptions thus using the predicate method. But as can be seen below it is returning inconsistent results. This behaviour causes some of my code written in a controller to fail.
$ rails c
Loading development environment (Rails 4.2.0)
2.2.1 :001 > defined?(User)
=> "constant"
2.2.1 :002 > defined?(AuthenticationToken)
=> nil
2.2.1 :003 > AuthenticationToken
=> AuthenticationToken (call 'AuthenticationToken.connection' to establish a connection)
2.2.1 :004 > defined?(AuthenticationToken)
=> "constant"
I also tried using Kernel.const_defined?
but same inconsistent behaviour is found.
$ rails c
Loading development environment (Rails 4.2.0)
2.2.1 :001 > Kernel.const_defined?('Role')
=> false
2.2.1 :002 > Kernel.const_defined?('AuthenticationToken')
=> false
2.2.1 :003 > AuthenticationToken
=> AuthenticationToken (call 'AuthenticationToken.connection' to establish a connection)
2.2.1 :004 > Kernel.const_defined?('AuthenticationToken')
=> true
Is there any standard way to make that code behave in consistent manner?
Thanks.