# Integer(nil) versus Float(nil) versus String(nil)

**URL:** <https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271>\
**Category:** ruby-talk\
**Created:** [23 August 2006 20:45 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271 "2006-08-23T20:45:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoffer\_Sawicki](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Christoffer\_Sawicki](https://rubytalk.org/u/Christoffer_Sawicki)\
**Post date:** [23 August 2006 20:45 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271/1 "2006-08-23T20:45:05Z")

</div>

Dear Rubyists,

def x() yield rescue :error end

[x { Integer(nil) }, x { Float(nil) }, x{ String(nil) }]  
# =\> [0, :error, ""]

Isn't that a bit inconsistent?

Cheers,

> **···**
>
> --  
> Christoffer Sawicki  
> [http://vemod.net/](http://vemod.net/)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 August 2006 21:30 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271/2 "2006-08-23T21:30:20Z")

</div>

Christoffer Sawicki wrote:

> Dear Rubyists,
> 
> def x() yield rescue :error end
> 
> [x { Integer(nil) }, x { Float(nil) }, x{ String(nil) }]  
> # =\> [0, :error, ""]
> 
> Isn't that a bit inconsistent?

Is this better?

irb(main):003:0\> nil.to\_i  
=\> 0  
irb(main):004:0\> nil.to\_f  
=\> 0.0  
irb(main):005:0\> nil.to\_s  
=\> ""

Cheers

&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [24 August 2006 21:30 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271/3 "2006-08-24T21:30:54Z")

</div>

Christoffer Sawicki wrote:

> Dear Rubyists,
> 
> def x() yield rescue :error end
> 
> [x { Integer(nil) }, x { Float(nil) }, x{ String(nil) }]  
> # =\> [0, :error, ""]
> 
> Isn't that a bit inconsistent?
> 
> Cheers,

Probably.

\<rant\>  
But my Java-addled brain makes me make damn sure nulls / nils don't come anywhere near I expect actual data, like into collections or numbers. If you don't rely on automagical conversion to work, it can't bite you if it doesn't. Just code explicitly.  
\</rant\>

The to\_foo and #Foo() type conversion methods being different always confuses the heck of me, which is why I get paranoid around them. Does anyone have a link to some rationale for and explanation of the difference?

David Vallner

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [23 August 2006 21:38 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271/4 "2006-08-23T21:38:12Z")

</div>

Well, I'd say that it confirms that the original case is an inconsistency.

> **···**
>
> On 8/23/06, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > Christoffer Sawicki wrote:  
> > \> Dear Rubyists,  
> > \>  
> > \> def x() yield rescue :error end  
> > \>  
> > \> [x { Integer(nil) }, x { Float(nil) }, x{ String(nil) }]  
> > \> # =\> [0, :error, ""]  
> > \>  
> > \> Isn't that a bit inconsistent?
> > 
> > Is this better?
> > 
> > irb(main):003:0\> nil.to\_i  
> > =\> 0  
> > irb(main):004:0\> nil.to\_f  
> > =\> 0.0  
> > irb(main):005:0\> nil.to\_s  
> > =\> ""
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Christoffer\_Sawicki](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Christoffer\_Sawicki](https://rubytalk.org/u/Christoffer_Sawicki)\
**Post date:** [28 August 2006 19:36 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271/5 "2006-08-28T19:36:24Z")

</div>

> Probably.
> 
> \<rant\>  
> But my Java-addled brain makes me make damn sure nulls / nils don't come  
> anywhere near I expect actual data, like into collections or numbers. If  
> you don't rely on automagical conversion to work, it can't bite you if  
> it doesn't. Just code explicitly.  
> \</rant\>

That's definitely a valid point, but slightly irrelevant.

> The to\_foo and #Foo() type conversion methods being different always  
> confuses the heck of me, which is why I get paranoid around them. Does  
> anyone have a link to some rationale for and explanation of the difference?

Integer/Float are usually considered to be the \*strict\* equivalents of  
to\_i/to\_f. Try to feed them non-number strings and you'll see. I don't  
know anything more about them though. 🙂

Anyway, I was a bit puzzled over these two things on Integer/Float:

1) Them accepting nil at all  
2) The (IMHO) inconsistency

I can accept both things as they are, since nothing says they should  
act the way I except them to, but I thought it could be good to bring  
it up.

Cheers,

> **···**
>
> On 8/24/06, David Vallner \<david@vallner.net\> wrote:
> 
> --  
> Christoffer Sawicki  
> [http://vemod.net/](http://vemod.net/)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [28 August 2006 19:59 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271/6 "2006-08-28T19:59:40Z")

</div>

> \> Probably.  
> \>  
> \> \<rant\>  
> \> But my Java-addled brain makes me make damn sure nulls / nils don't come  
> \> anywhere near I expect actual data, like into collections or numbers. If  
> \> you don't rely on automagical conversion to work, it can't bite you if  
> \> it doesn't. Just code explicitly.  
> \> \</rant\>
> 
> That's definitely a valid point, but slightly irrelevant.

Well it is your thread but I understand and agree with David that the nil  
(do not call it null on that list though 😉  
in a conversion is troubeling.  
I read him that way that by banning it a part of your inconsistency will go  
away, do you agree?

> The to\_foo and #Foo() type conversion methods being different always  
> \> confuses the heck of me, which is why I get paranoid around them. Does  
> \> anyone have a link to some rationale for and explanation of the  
> difference?
> 
> Integer/Float are usually considered to be the \*strict\* equivalents of  
> to\_i/to\_f.

Are they? So far I have never heared that claim.  
[module Kernel - RDoc Documentation](http://www.ruby-doc.org/core/classes/Kernel.html#M002003) says the contrary.  
I do not necessarily think that is good, but that is how it is documented.

Try to feed them non-number strings and you'll see.

No you will not, "x".to\_i Integer("x")

I don't

> know anything more about them though. 🙂
> 
> Anyway, I was a bit puzzled over these two things on Integer/Float:
> 
> 1) Them accepting nil at all  
> 2) The (IMHO) inconsistency
> 
> I can accept both things as they are, since nothing says they should  
> act the way I except them to, but I thought it could be good to bring  
> it up.

I too hope that behavior will go away.

Cheers,

> --  
> Christoffer Sawicki  
> [http://vemod.net/](http://vemod.net/)

Robert

> **···**
>
> On 8/28/06, Christoffer Sawicki \<christoffer.sawicki@gmail.com\> wrote:
> 
> > On 8/24/06, David Vallner \<david@vallner.net\> wrote:
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Christoffer\_Sawicki](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Christoffer\_Sawicki](https://rubytalk.org/u/Christoffer_Sawicki)\
**Post date:** [2 September 2006 18:28 UTC](https://rubytalk.org/t/integer-nil-versus-float-nil-versus-string-nil/30271/7 "2006-09-02T18:28:23Z")

</div>

Hello!

> \>  
> \> \> Probably.  
> \> \>  
> \> \> \<rant\>  
> \> \> But my Java-addled brain makes me make damn sure nulls / nils don't come  
> \> \> anywhere near I expect actual data, like into collections or numbers. If  
> \> \> you don't rely on automagical conversion to work, it can't bite you if  
> \> \> it doesn't. Just code explicitly.  
> \> \> \</rant\>  
> \>  
> \> That's definitely a valid point, but slightly irrelevant.
> 
> Well it is your thread but I understand and agree with David that the nil  
> (do not call it null on that list though 😉  
> in a conversion is troubeling.  
> I read him that way that by banning it a part of your inconsistency will go  
> away, do you agree?

Yes, I agree. (But the inconsistency is still there.) 🙂

Just for the record, I discovered the behaviour when using FasterCSV  
(that emits nil for empty cells) and Integer(x) as a kind of  
assertion.

> \> The to\_foo and #Foo() type conversion methods being different always  
> \> \> confuses the heck of me, which is why I get paranoid around them. Does  
> \> \> anyone have a link to some rationale for and explanation of the  
> \> difference?  
> \>  
> \> Integer/Float are usually considered to be the \*strict\* equivalents of  
> \> to\_i/to\_f.
> 
> Are they? So far I have never heared that claim.  
> [http://www.ruby-doc.org/core/classes/Kernel.html#M002003](http://www.ruby-doc.org/core/classes/Kernel.html#M002003) says the contrary.  
> I do not necessarily think that is good, but that is how it is documented.

When discussing this issue on #ruby-lang the general concensus was  
that Integer/Float are indeed stricter versions of to\_i/to\_f. The  
Pickaxe says "A call to Integer will work wonders (and will throw an  
exception if the input isn't a well-formed integer)". I should also  
note that the documentation for Kernel#Integer in the Pickaxe actually  
has "Integer(nil) =\> 0" as an example.

> Try to feed them non-number strings and you'll see.
> 
> No you will not, "x".to\_i Integer("x")

"x".to\_i =\> 0  
Integer("x") # ArgumentError: invalid value for Integer: "x"

Isn't that just what I meant?

> \> I don't  
> \> know anything more about them though. 🙂  
> \>  
> \> Anyway, I was a bit puzzled over these two things on Integer/Float:  
> \>  
> \> 1) Them accepting nil at all  
> \> 2) The (IMHO) inconsistency  
> \>  
> \> I can accept both things as they are, since nothing says they should  
> \> act the way I except them to, but I thought it could be good to bring  
> \> it up.
> 
> I too hope that behavior will go away.

Thanks,

> **···**
>
> On 8/28/06, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > On 8/28/06, Christoffer Sawicki \<christoffer.sawicki@gmail.com\> wrote:  
> > \> On 8/24/06, David Vallner \<david@vallner.net\> wrote:
> 
> --  
> Christoffer Sawicki  
> [http://vemod.net/](http://vemod.net/)
