
Issue #19884 has been updated by sawa (Tsuyoshi Sawada). sawa (Tsuyoshi Sawada) wrote:
You stated that you want &. to work on classes, but that does not make sense. If ActiveRecord::Base is not defined, it is not a class. How would Ruby know that it is (supposed to be) a class?
p8 (Petrik de Heus) wrote:
`defined?` also handles undefined classes.
sawa (Tsuyoshi Sawada) wrote:
Can you show an example of that?
p8 (Petrik de Heus) wrote:
```ruby defined?(UndefinedConstant) # returns nil instead of raising an error ```
Where does it say that `UndefinedConstant` is an "undefined **class** " rather than an undefined constant? ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104849 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError: ```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ``` In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it: ```ruby defined?(OptionalDependency) && OptionalDependency.some_method ``` Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well. ```ruby ClassThatMightNotExist&.some_method ``` -- https://bugs.ruby-lang.org/