nil.to_s != "nil"

E

Eero Saynatkari

--TlGginnuNEm5PkhK
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Is it possible to change NilClass#to_s to return "nil"
rather than "" since "" != nil?

--TlGginnuNEm5PkhK
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFQY9Y7Nh7RM4TrhIRAm9hAKDaXtGZOv7S8oxuquU3/zZLIo/PygCg6l4W
CQWOY+lfi3PGeoET3mA/IRM=
=01lv
-----END PGP SIGNATURE-----

--TlGginnuNEm5PkhK--
 
D

Dmitri Priimak

Eero said:
Is it possible to change NilClass#to_s to return "nil"
rather than "" since "" != nil?
-- test.rb BEGIN
p nil.to_s.nil?

class NilClass
def to_s
nil
end
end

p nil
p nil.to_s.nil?
-- test.rb END

$ ruby test.rb
false
nil
true
 
E

Eero Saynatkari

--flDc4cbvG5Y7qIjC
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Eero Saynatkari wrote:
=20
-- test.rb BEGIN
p nil.to_s.nil?
=20
class NilClass
def to_s
nil
end
end
=20
p nil
p nil.to_s.nil?
-- test.rb END
=20
$ ruby test.rb
false
nil
true

I should have been clearer. I was wondering if it could
be fixed in the language itself :) Thank you though!

--flDc4cbvG5Y7qIjC
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFQZKU7Nh7RM4TrhIRAmV4AJ9XwE/OchHZggB90B2/g/rb6k+TXwCeJvLw
Vv+QnLHwXqKYyi5XT0qe9Yg=
=tlyt
-----END PGP SIGNATURE-----

--flDc4cbvG5Y7qIjC--
 
J

Justin Collins

Paul said:
Eero Saynatkari wrote:



#!/usr/bin/ruby -w

class NilClass
def to_s
return "nil"
end
end

p nil.to_s

"nil"

You may not want to do this. There might be a reason for the present
behavior. But it is very easy to do, as are all such changes in Ruby.

I don't know what the real reason is, but it can be very useful when
interpolating strings to have it be ""

-Justin
 
P

Phrogz

Eero said:
Is it possible to change NilClass#to_s to return "nil"
rather than "" since "" != nil?

1) #to_s should always return a string.
2) Any string representation will always be a truth value.

The same situation exists for false:
!!false.to_s #=> true

You are correct that "" != nil, but "nil" != nil also.
What are you really trying to achieve? And, did you know that
nil.inspect yields "nil"?
 
E

Eero Saynatkari

--bWTBxx0QKOXH2iZ2
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=20
1) #to_s should always return a string.
Right.

2) Any string representation will always be a truth value.
Yep.

The same situation exists for false:
!!false.to_s #=3D> true

true.to_s # =3D> "true"
false.to_s # =3D> "false"
nil.to_s # =3D> ""

"" is not a valid representation of nil. "nil" is.
You are correct that "" !=3D nil, but "nil" !=3D nil also.
What are you really trying to achieve? And, did you know that
nil.inspect yields "nil"?

Yes. I want consistency.

--bWTBxx0QKOXH2iZ2
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFQjN57Nh7RM4TrhIRAr8TAJ0UFae1AFHsxPJ4H9naDEt5iXXHpQCg67Lh
KSvdnW6VJyAJcLYvi9cw93M=
=WUWD
-----END PGP SIGNATURE-----

--bWTBxx0QKOXH2iZ2--
 
L

Logan Capaldo

problem = nil

"but this is a #{ problem }"
problem = ''

"is it really a #{ problem }?"

Of course there's _tons_ of code out there that relies on nil.to_s being
the empty string. I don't expect it will change anytime soon.
 
V

Vidar Hokstad

Eero said:
Yes. I want consistency.

It is consistent. "to_s" returns a string representation of the
value(s) of the object you call it on.

The value of the object nil is _nothing_ and the empty string is a
sensible representation of that.

Vidar
 
F

Florian Frank

Eero said:
Yes. I want consistency.
Ruby's behavior is consistent:
"a" + nil.to_s # => "a"
1 + nil.to_i # => 1
1.0 + nil.to_f # => 1.0
[1] + nil.to_a
# => [1]

nil.to_X always returns the neutral element for #+. Of course Matz could
have chosen some other kind of consistency instead of this one.
 
M

Morton Goldberg

It is consistent. "to_s" returns a string representation of the
value(s) of the object you call it on.

The value of the object nil is _nothing_ and the empty string is a
sensible representation of that.

As well as sensible, it can be useful:

<code>
#! /usr/bin/env ruby -w

def bottles(n)
"#{n} bottle#{'s' if n != 1} of beer on the wall"
end

bottles(99) # => "99 bottles of beer on the wall"
bottles(1) # => "1 bottle of beer on the wall"
</code>

Regards, Morton
 
G

Gregory Brown

Of course there's _tons_ of code out there that relies on nil.to_s being
the empty string. I don't expect it will change anytime soon.

Besides, we already have syntax that prevents this from being terribly
ugly anyway.
=> true


I like knowing the difference between an empty string and an undefined value.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top