From: Yaw Boakye via ruby-core Date: 2023-10-08T20:29:29+01:00 Subject: [ruby-core:114982] Re: [Ruby master Feature#19884] Make Safe Navigation Operator work on classes --===============0947296850977986587== Content-Type: multipart/alternative; boundary="000000000000b55c0b0607397e0f" --000000000000b55c0b0607397e0f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable How may `defined!?` be different from `Object#const_get` which is able to lookup and load a constant given its name? On Sun 8 Oct 2023 at 11:41, p8 (Petrik de Heus) via ruby-core < ruby-core@ml.ruby-lang.org> wrote: > 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=E2=81=89=EF=B8=8F` for now. > > Isn't this what you want? > > > > ```ruby > > defined=E2=81=89=EF=B8=8F(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 constan= t > 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/ --000000000000b55c0b0607397e0f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
How may `defined!?` be different from `Object#const_get` = which is able to lookup and load a constant given its name?

<= div class=3D"gmail_quote">
On Sun 8 Oc= t 2023 at 11:41, p8 (Petrik de Heus) via ruby-core <ruby-core@ml.ruby-lang.org> wrote:
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 (i= nstead of the expression type string) or nil.
>
> Let's call it `defined=E2=81=89=EF=B8=8F` for now.
> Isn't this what you want?
>
> ```ruby
> defined=E2=81=89=EF=B8=8F(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 (Nam= eError)
```

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 NoMethodEr= ror exceptions when calling methods on objects that may be nil.
It would be nice if we could use the Safe Navigation Operator to avoid Name= Error on undefined constants as well.

```ruby
ClassThatMightNotExist&.some_method

```



--
https://bugs.ruby-lang.org/
=C2=A0______________________________________________
=C2=A0ruby-core mailing list -- ruby-core@ml.ruby-lang.org
=C2=A0To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
=C2=A0ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.o= rg/
--000000000000b55c0b0607397e0f-- --===============0947296850977986587== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ______________________________________________ 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/ --===============0947296850977986587==--