How may `defined!?` be different from `Object#const_get` which is able to lookup and load a constant given its name?
Issue #19884 has been updated by p8 (Petrik de Heus).
nobu (Nobuyoshi Nakada) wrote in #note-10:
> 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
> ```
Yes, that would be nice.
----------------------------------------
Feature #19884: Make Safe Navigation Operator work on classes
https://bugs.ruby-lang.org/issues/19884#change-104853
* 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/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/