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

**URL:** <https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203>\
**Category:** Ruby\
**Created:** [August 23, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203 "2006-08-23T22:46:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [August 23, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203/1 "2006-08-23T22:46:34Z")

</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,

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [August 23, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203/2 "2006-08-23T23:32:38Z")

</div>

Christoffer S. 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

```
robert
```

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [August 23, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203/3 "2006-08-23T23:38:39Z")

</div>

On 8/23/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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  
> =\> “”

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

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [August 28, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203/4 "2006-08-28T21:38:19Z")

</div>

On 8/24/06, David V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Probably.
> 
> 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.

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,

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [August 24, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203/5 "2006-08-24T23:31:20Z")

</div>

Christoffer S. wrote:

> 

Probably.

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.

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 V.

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [August 28, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203/6 "2006-08-28T22:00:22Z")

</div>

On 8/28/06, Christoffer S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 S.  
> [http://vemod.net/](http://vemod.net/)

Robert

–  
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\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [September 2, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/integer-nil-versus-float-nil-versus-string-nil/66203/7 "2006-09-02T20:32:56Z")

</div>

Hello!

On 8/28/06, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > 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.

> [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.

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?

> > it up.
> 
> I too hope that behavior will go away.

Thanks,
