a == nil or a.nil?

A

ako...

hello,

this is more of a philosophical question. my C experience makes me feel
more comfortable with expressions such as 'a == nil' rather than
a.nil?. i feel strange when i invoke a method on a nil value. i know,
nil is also an object and not just a zero reference but still it feels
strange...

would the experts please comment on why one way would be preferable
over the other? also, i am curious to know why nil? has been
implemented at all? is '== nil' not enough?

thanks
konstantin
 
J

JB Eriksson

------=_Part_34024_15845667.1132701386940
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

hello,

this is more of a philosophical question. my C experience makes me feel
more comfortable with expressions such as 'a =3D=3D nil' rather than
a.nil?. i feel strange when i invoke a method on a nil value. i know,
nil is also an object and not just a zero reference but still it feels
strange...

would the experts please comment on why one way would be preferable
over the other? also, i am curious to know why nil? has been
implemented at all? is '=3D=3D nil' not enough?

thanks
konstantin
for readability, I wager.

------=_Part_34024_15845667.1132701386940--
 
E

Eric Mahurin

hello,

this is more of a philosophical question. my C experience makes me feel
more comfortable with expressions such as 'a =3D=3D nil' rather than
a.nil?. i feel strange when i invoke a method on a nil value. i know,
nil is also an object and not just a zero reference but still it feels
strange...

would the experts please comment on why one way would be preferable
over the other? also, i am curious to know why nil? has been
implemented at all? is '=3D=3D nil' not enough?

thanks
konstantin

=3D=3D is usually meant to query whether the data in the objects are the
same (or they are the same object). For most =3D=3D methods, if the data
is not comparable (not the same class), it will return false rather
than raise an exception. So... =3D=3D nil should work fine in most cases.

I would use one of these which are more direct:

a.nil?
nil=3D=3Da # uses the NilClass#=3D=3D rather than an arbitrary =3D=3D
a.equal?(a)
nil.equal?(a)
 
B

Bill Kelly

From: "ako... said:
this is more of a philosophical question. my C experience makes me feel
more comfortable with expressions such as 'a == nil' rather than
a.nil?. i feel strange when i invoke a method on a nil value.

Another thing that would be strange in C is:

5.times {print "Hello!"}

:) But to me, it's not worth embracing C's ideosyncracies when programming
in Ruby. The "everything is an object" consistency of Ruby is an elegant
and enjoyable part of the language.

As an aside, if you're coming from C, you might be surprised that:

a = 0 # zero
print "it's true!" if a # => prints "it's true!"

Zero is not false in Ruby.


Regards,

Bill
 
L

Lionel Thiry

ako... a écrit :
hello,

this is more of a philosophical question. my C experience makes me feel
more comfortable with expressions such as 'a == nil' rather than
a.nil?. i feel strange when i invoke a method on a nil value. i know,
nil is also an object and not just a zero reference but still it feels
strange...

would the experts please comment on why one way would be preferable
over the other? also, i am curious to know why nil? has been
implemented at all? is '== nil' not enough?

thanks
konstantin

By the way,
a == nil
is also a method call. It is actually equivalent to:
a.==(nil)

If nil has a == method, why not adding some other practical methods like
nil? ? Actually, nil? is probably a bit faster than ==(nil).
 
R

Rob Rypka

this is more of a philosophical question. my C experience makes me feel
more comfortable with expressions such as 'a =3D=3D nil' rather than
a.nil?. i feel strange when i invoke a method on a nil value. i know,
nil is also an object and not just a zero reference but still it feels
strange...

If you're not feeling the nil methods, remember nil is the only
Boolean false (besides FalseClass). So, unless you specifically need
to differentiate between false and nil, you can do this:

if !a
#stuff
end

That's something I see a lot in C, as NULL =3D=3D false. You might feel
more at home, and save a method call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top