From: Matthew Kerwin Date: 2015-10-23T07:19:47+10:00 Subject: [ruby-core:71166] Re: [Ruby trunk - Feature #11537] Introduce "Safe navigation operator" --001a113a3e0afc39130522b80d3b Content-Type: text/plain; charset=UTF-8 On 23/10/2015 2:46 AM, wrote: > > Issue #11537 has been updated by Jeremy Evans. > > > Tom Reznick wrote: > > Hi, > > > > I think we may have found some unexpected behavior with the `.?` operator. > > > > If I call the following: > > > > s = Struct.new(:x) > > o = s.new() > > o.x #=> nil > > o.x.nil? #=> true > > o.x.?nil? #=> nil > > o.x.kind_of?(NilClass) #=> true > > o.x.?kind_of?(NilClass) #=> nil > > o.x.methods.include?(:nil?) #=> true > > > > > > While it's arguably a bit peculiar to try to check that `nil` is `nil`, in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably return `true`. > > I think it's completely expected that `nil.?kind_of?(NilClass)` returns `nil` and not `true`. The whole point of `.?` is to return `nil` without calling the method if the receiver is `nil`. I'm not sure if `.?` is a good idea syntax-wise, but if you are going to have it, it shouldn't have special cases for specific methods. > I agree, or put another way: if you're testing for nil in two ways, .? has higher priority. That makes it a programmer issue, not a ruby one. --001a113a3e0afc39130522b80d3b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On 23/10/2015 2:46 AM, <merch-redmine@jeremyevans.net> wrote:
>
> Issue #11537 has been updated by Jeremy Evans.
>
>
> Tom Reznick wrote:
> > Hi,
> >
> > I think we may have found some unexpected behavior with the `.?` = operator.
> >
> > If I call the following:
> >
> >=C2=A0 =C2=A0 =C2=A0s =3D Struct.new(:x)
> >=C2=A0 =C2=A0 =C2=A0o =3D s.new()
> >=C2=A0 =C2=A0 =C2=A0o.x #=3D> nil
> >=C2=A0 =C2=A0 =C2=A0o.x.nil? #=3D> true
> >=C2=A0 =C2=A0 =C2=A0o.x.?nil? #=3D> nil
> >=C2=A0 =C2=A0 =C2=A0o.x.kind_of?(NilClass) #=3D> true
> >=C2=A0 =C2=A0 =C2=A0o.x.?kind_of?(NilClass) #=3D> nil
> >=C2=A0 =C2=A0 =C2=A0o.x.methods.include?(:nil?) #=3D> true
> >
> >
> > While it's arguably a bit peculiar to try to check that `nil`= is `nil`, in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably ret= urn `true`.
>
> I think it's completely expected that `nil.?kind_of?(NilClass)` re= turns `nil` and not `true`.=C2=A0 The whole point of `.?` is to return `nil= ` without calling the method if the receiver is `nil`.=C2=A0 I'm not su= re if `.?` is a good idea syntax-wise, but if you are going to have it, it = shouldn't have special cases for specific methods.
>

I agree, or put another way: if you're testing for nil i= n two ways, .? has higher priority. That makes it a programmer issue, not a= ruby one.

--001a113a3e0afc39130522b80d3b--