
Issue #19884 has been updated by nobu (Nobuyoshi Nakada). What you wan doesn't seem the extension of the safe navigation operator. p8 (Petrik de Heus) wrote:
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
```
Rather the extension of `defined?` operator which returns the value (instead of the expression type string) or nil. Let's call it `defined⁉️` for now. Isn't this what you want? ```ruby defined⁉️(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
```
This can't work because `&.` will works on the **result** of the LHS, which might have raised already. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104852 * 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/