[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

nil.to_s != "nil"

Eero Saynatkari

10/27/2006 4:50:00 AM

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

Dmitri Priimak

10/27/2006 4:56:00 AM

0

Eero Saynatkari wrote:

>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


--
Dmitri Priimak

Paul Lutus

10/27/2006 4:57:00 AM

0

Eero Saynatkari wrote:

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

#!/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.

--
Paul Lutus
http://www.ara...

Eero Saynatkari

10/27/2006 5:04:00 AM

0

On 2006.10.27 13:56, Dmitri Priimak wrote:
> Eero Saynatkari wrote:
>
> >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

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

Justin Collins

10/27/2006 5:15:00 AM

0

Paul Lutus wrote:
> Eero Saynatkari wrote:
>
>
>> Is it possible to change NilClass#to_s to return "nil"
>> rather than "" since "" != nil?
>>
>
> #!/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

Gavin Kistner

10/27/2006 3:16:00 PM

0

Eero Saynatkari wrote:
> 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"?

Eero Saynatkari

10/27/2006 4:30:00 PM

0

On 2006.10.28 00:20, Phrogz wrote:
> Eero Saynatkari wrote:
> > Is it possible to change NilClass#to_s to return "nil"
> > rather than "" since "" != nil?
>
> 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 #=> true

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

"" is not a valid representation of nil. "nil" is.

> 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"?

Yes. I want consistency.

Ara.T.Howard

10/27/2006 4:36:00 PM

0

Logan Capaldo

10/28/2006 4:44:00 PM

0

On Sat, Oct 28, 2006 at 01:35:48AM +0900, ara.t.howard@noaa.gov wrote:
> On Sat, 28 Oct 2006, Eero Saynatkari wrote:
>
> >>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"?
> >
> >Yes. I want consistency.
>
> 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.

vidar

10/28/2006 7:00:00 PM

0


Eero Saynatkari wrote:
> On 2006.10.28 00:20, Phrogz wrote:
> > 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"?
>
> 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

Florian Frank

10/28/2006 7:24:00 PM

0

Eero Saynatkari wrote:
>> 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"?
>>
>
> 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.

--
Florian Frank