[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

nil.to_i returning zero

Gerardo Santana Gómez Garrido

10/14/2007 7:41:00 PM

zero in Ruby is true, not false, in a boolean context.

What does false.to_i return? An exception. There is not a numeric
interpretation for false.

What about nil. nil is nothing. The only other object that evaluates
to false in a boolean context.

What does nil.to_i return? Zero. And I wonder why. How can nil be
interpreted as a number. It's beyond me.

Anyone care to explain it to me?

So far I've found the following links about the issue:

http://www.oreillynet.com/ruby/blog/2005/12/is_this_a_good_thing_o...
http://journal.dedasys.com/articles/2007/05/08/rails-bitten-by-ruby...
http://groups.google.com/group/ruby-talk-google/browse_thread/thread/a475bb8a541f700e/b9f0cac6945a4210?lnk=gst&q=nil.to_i#b9f0ca...

--
Gerardo Santana

43 Answers

Axel Etzold

10/14/2007 7:52:00 PM

0

> What does nil.to_i return? Zero. And I wonder why. How can nil be
> interpreted as a number. It's beyond me.
>
> Anyone care to explain it to me?

Dear Gerardo,

without claiming any particular authority, to me, this definition
of what nil.to_i should return doesn't violate the principle of least
surprise so cherished by Ruby coders - if you have nil dollars money, that's 0 dollars -- think of the balance of somebody who hasn't
opened an account at "Ruby Deposit Bank" yet --
but if you wanted nil.to_i to raise an exception,
because the "Ruby-Using Logicians Club" might be unhappy with such
elliptic thinking, I might find that reasonable also.
Maybe one has to distinguish between 0 and 1 on the one hand and
false and true on the other, when coding Ruby, right from the start,
to avoid any confusion.

Best regards,

Axel
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/s...

John Joyce

10/14/2007 9:00:00 PM

0

nil is nothing?
well, by some thinking zero is nothing as well.
nil.to_i returning zero is convenient.


If it returned nil, then you'd get exceptions and it would be
pointless to have nil respond to to_i
Sure, nil.to_i returning nil could also make sense in a way, but it
is really a matter of making decisions about design.

It's just the way the language is.

It's not C or C++ or another language. It's Ruby.
Every language does things its own way.
this is one of those things.

Jeremy McAnally

10/15/2007 3:31:00 AM

0

I expect to get a converted object or some *Error (TypeError maybe?)
back from any to_* method. Giving me the same object back or nil
doesn't make any sense...

As others have noted, I think the current behavior makes sense: I have
nil (whatever...money, cats, etc.) I'd think an integer representation
would be 0.

--Jeremy

On 10/14/07, Gerardo Santana Gómez Garrido <gerardo.santana@gmail.com> wrote:
> zero in Ruby is true, not false, in a boolean context.
>
> What does false.to_i return? An exception. There is not a numeric
> interpretation for false.
>
> What about nil. nil is nothing. The only other object that evaluates
> to false in a boolean context.
>
> What does nil.to_i return? Zero. And I wonder why. How can nil be
> interpreted as a number. It's beyond me.
>
> Anyone care to explain it to me?
>
> So far I've found the following links about the issue:
>
> http://www.oreillynet.com/ruby/blog/2005/12/is_this_a_good_thing_o...
> http://journal.dedasys.com/articles/2007/05/08/rails-bitten-by-ruby...
> http://groups.google.com/group/ruby-talk-google/browse_thread/thread/a475bb8a541f700e/b9f0cac6945a4210?lnk=gst&q=nil.to_i#b9f0ca...
>
> --
> Gerardo Santana
>
>


--
http://www.jeremymca...

My books:
Ruby in Practice
http://www.manning.com...

My free Ruby e-book
http://www.humblelittlerub...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Chad Perrin

10/15/2007 5:30:00 AM

0

On Mon, Oct 15, 2007 at 04:40:35AM +0900, Gerardo Santana G?mez Garrido wrote:
>
> What does nil.to_i return? Zero. And I wonder why. How can nil be
> interpreted as a number. It's beyond me.

There's been a few attempts to answer the question already. I'm curious,
though:

What would you expect it to return, and why would you think that should
be the expected behavior?

Nothing immediately occurs to me as a better result of evaluating
nil.to_i than 0, within the context of the Ruby language, but if you have
an alternative view I'm open to learning from it.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Patrick J. LoPresti: "Emacs has been replaced by a shell script which 1)
Generates a syslog message at level LOG_EMERG; 2) reduces the user's disk
quota by 100K; and 3) RUNS ED!!!!!!"

Michael Fellinger

10/15/2007 7:03:00 AM

0

On 10/15/07, Chad Perrin <perrin@apotheon.com> wrote:
> On Mon, Oct 15, 2007 at 04:40:35AM +0900, Gerardo Santana G?mez Garrido wrote:
> >
> > What does nil.to_i return? Zero. And I wonder why. How can nil be
> > interpreted as a number. It's beyond me.
>
> There's been a few attempts to answer the question already. I'm curious,
> though:
>
> What would you expect it to return, and why would you think that should
> be the expected behavior?
>
> Nothing immediately occurs to me as a better result of evaluating
> nil.to_i than 0, within the context of the Ruby language, but if you have
> an alternative view I'm open to learning from it.

nil.to_i
# > NoMethodError: undefined method `to_i' for nil:NilClass
Integer(nil)
# > TypeError: can't convert nil into Integer

This is on my wish list for Christmas among other things.

Konrad Meyer

10/15/2007 8:12:00 AM

0

Quoth Michael Fellinger:
> On 10/15/07, Chad Perrin <perrin@apotheon.com> wrote:
> > On Mon, Oct 15, 2007 at 04:40:35AM +0900, Gerardo Santana G?mez Garrido
wrote:
> > >
> > > What does nil.to_i return? Zero. And I wonder why. How can nil be
> > > interpreted as a number. It's beyond me.
> >
> > There's been a few attempts to answer the question already. I'm curious,
> > though:
> >
> > What would you expect it to return, and why would you think that should
> > be the expected behavior?
> >
> > Nothing immediately occurs to me as a better result of evaluating
> > nil.to_i than 0, within the context of the Ruby language, but if you have
> > an alternative view I'm open to learning from it.
>
> nil.to_i
> # > NoMethodError: undefined method `to_i' for nil:NilClass
> Integer(nil)
> # > TypeError: can't convert nil into Integer
>
> This is on my wish list for Christmas among other things.

Thing is, #to_i explicitly does *not* throw errors -- that's what Integer() is
for. Current (1.8.6) ruby doesn't error on Integer(nil) either, but IMO it
should.

Regards,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Trans

10/15/2007 8:59:00 AM

0



On Oct 14, 3:40 pm, "Gerardo Santana Gómez Garrido"
<gerardo.sant...@gmail.com> wrote:
> zero in Ruby is true, not false, in a boolean context.
>
> What does false.to_i return? An exception. There is not a numeric
> interpretation for false.
>
> What about nil. nil is nothing. The only other object that evaluates
> to false in a boolean context.
>
> What does nil.to_i return? Zero. And I wonder why. How can nil be
> interpreted as a number. It's beyond me.
>
> Anyone care to explain it to me?

In the strictest sense of what's "proper" I suppose you are right, it
ought return a NoMethodError. However more often than not it seems to
be exactly what we would want to happen anyway. If it were not for
that convenience we'd have a bunch of these all over the place:

(x ? x.to_i : 0)


Following through with this, we also have to_s => "", to_a => [] and
to_f => 0.0. And I support adding to_h => {}.

T.


Robert Dober

10/15/2007 10:28:00 AM

0

On 10/15/07, Konrad Meyer <konrad@tylerc.org> wrote:
> Quoth Michael Fellinger:
> > On 10/15/07, Chad Perrin <perrin@apotheon.com> wrote:
> > > On Mon, Oct 15, 2007 at 04:40:35AM +0900, Gerardo Santana G?mez Garrido
> wrote:
> > > >
> > > > What does nil.to_i return? Zero. And I wonder why. How can nil be
> > > > interpreted as a number. It's beyond me.
> > >
> > > There's been a few attempts to answer the question already. I'm curious,
> > > though:
> > >
> > > What would you expect it to return, and why would you think that should
> > > be the expected behavior?
> > >
> > > Nothing immediately occurs to me as a better result of evaluating
> > > nil.to_i than 0, within the context of the Ruby language, but if you have
> > > an alternative view I'm open to learning from it.
> >
> > nil.to_i
> > # > NoMethodError: undefined method `to_i' for nil:NilClass
> > Integer(nil)
> > # > TypeError: can't convert nil into Integer
> >
> > This is on my wish list for Christmas among other things.
>
> Thing is, #to_i explicitly does *not* throw errors -- that's what Integer() is
> for. Current (1.8.6) ruby doesn't error on Integer(nil) either, but IMO it
> should.
But Michael did not ask NilClass#to_i to throw an error, he did ask it
to go away :)

Now I am completely impartial on the behavior, but if nil.to_i is
confusing for some folks it might be a good idea to put it away unless
other folks really need it.
My pragmatic question is therefore:
nil.to_i usecases anyone?

Cheers
Robert
--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Robert Dober

10/15/2007 10:32:00 AM

0

On 10/15/07, Trans <transfire@gmail.com> wrote:
>
>
> On Oct 14, 3:40 pm, "Gerardo Santana Gómez Garrido"
> <gerardo.sant...@gmail.com> wrote:
> > zero in Ruby is true, not false, in a boolean context.
> >
> > What does false.to_i return? An exception. There is not a numeric
> > interpretation for false.
> >
> > What about nil. nil is nothing. The only other object that evaluates
> > to false in a boolean context.
> >
> > What does nil.to_i return? Zero. And I wonder why. How can nil be
> > interpreted as a number. It's beyond me.
> >
> > Anyone care to explain it to me?
>
> In the strictest sense of what's "proper" I suppose you are right, it
> ought return a NoMethodError. However more often than not it seems to
> be exactly what we would want to happen anyway. If it were not for
> that convenience we'd have a bunch of these all over the place:
>
> (x ? x.to_i : 0)
>
>
> Following through with this, we also have to_s => "", to_a => [] and
> to_f => 0.0. And I support adding to_h => {}.
Hmm I really like that to_a will go away for everything than Enumerations
I think therefore that to_h is not a good idea.

You know we have Facets for this kind of stuff, you might want to have
a look at it one day ;)
R.

--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Joachim Glauche

10/15/2007 10:40:00 AM

0

Robert Dober wrote:

> My pragmatic question is therefore:
> nil.to_i usecases anyone?

I remember using it somewhere. I think I used it for catching user input
integer values like
unless val.to_i == 0; ... ; end


The nil.to_something behaviour seems logical to me:

nil.to_i = Returns the integer which represents "nothing" = 0
nil.to_s = Returns the string which represents "nothing" = ""
...
--
Posted via http://www.ruby-....