Project

General

Profile

Feature #19884

Updated by p8 (Petrik de Heus) 7 months ago

If a constant isn't class might not be 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 need to add a method on it: conditional: 

 ```ruby 
 defined?(OptionalDependency) defined?(ActiveRecord::Base) && OptionalDependency.some_method ActiveRecord::Base.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. instead. 

 ```ruby 
 ClassThatMightNotExist&.some_method ActiveRecord::Base&.some_method 

 ```

Back