# No exceptions from String#to\_i

**URL:** <https://rubytalk.org/t/no-exceptions-from-string-to-i/372>\
**Category:** ruby-talk\
**Created:** [24 June 2002 11:37 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372 "2002-06-24T11:37:33Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [24 June 2002 11:37 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/1 "2002-06-24T11:37:33Z")

</div>

I’ve been bitten by this before… maybe  
it is a feature, but I’m not sure of the  
reasoning.

I wrote a line like this:

num = gets.chomp.to\_i rescue max

thinking that a simple carriage return  
would assign max to num.

But of course, “”.to\_i does not give us  
an error; it simply returns zero (as does  
"foo".to\_i or any other such string).

What is the reason for this? My instinct  
is that an exception should result.

I know it’s been discussed before, but I  
can’t find it now.

Thanks,  
Hal Fulton

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [24 June 2002 11:40 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/2 "2002-06-24T11:40:20Z")

</div>

> I know it's been discussed before, but I  
> can't find it now.

use #Integer if you want an error

pigeon% ruby -e 'Integer("")'  
-e:1:in `Integer': invalid value for Integer: "" (ArgumentError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -e:1  
pigeon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [24 June 2002 11:52 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/3 "2002-06-24T11:52:49Z")

</div>

Ah, thank you, I remember that now.

Which raises the question: Why is this  
“non-OO”? Why not str.integer (for instance)  
instead of Integer(str)? Is it just to make  
it more universally available (i.e., to  
different data types, Integer(non\_str\_obj))?

And why is Integer capitalized? I know of  
only four methods in the entire Ruby core  
that start with capital letters.

I sound like a five year old, do I not?  
Always asking why… 🙂

Thanks,  
Hal Fulton

> **···**
>
> ----- Original Message -----  
> From: “ts” [decoux@moulon.inra.fr](mailto:decoux@moulon.inra.fr)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Cc: [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Monday, June 24, 2002 6:40 AM  
> Subject: Re: No exceptions from String#to\_i
> 
> > > I know it’s been discussed before, but I  
> > > can’t find it now.
> > 
> > use #Integer if you want an error
> > 
> > pigeon% ruby -e ‘Integer(“”)’  
> > -e:1:in `Integer’: invalid value for Integer: “” (ArgumentError)  
> > from -e:1  
> > pigeon%

---

<div class="post-metadata">

**Author:** ![Nikodemus\_Siivola](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nikodemus\_Siivola](https://rubytalk.org/u/Nikodemus_Siivola)\
**Post date:** [24 June 2002 12:12 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/4 "2002-06-24T12:12:00Z")

</div>

More whys and wherefore’s…

I find this mildly strange:

Integer(‘’) #-\> exception

Integer(nil) == 0  
Integer(false) #-\> exception

Integer(:foo) == 14145  
Integer(‘foo’) #-\> exception

Is this ‘jus the way it is’, or is there a reason? I suppose symbols get  
hashed, but then why not apply the same logic to strings as well?

Also, since nil and false are the only non-true values, I would expect  
them to be treated the same in such a case.

Further strangeness:

Why ?

Array(foo) == foo.to\_a  
String(foo) == foo.to\_s  
Integer(foo) != foo.to\_i  
Float(foo) != foo.to\_f

– Nikodemus

> **···**
>
> On Mon, 24 Jun 2002, ts wrote:
> 
> > use #Integer if you want an error

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [24 June 2002 12:04 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/5 "2002-06-24T12:04:07Z")

</div>

> Which raises the question: Why is this  
> "non-OO"? Why not str.integer (for instance)  
> instead of Integer(str)? Is it just to make  
> it more universally available (i.e., to  
> different data types, Integer(non\_str\_obj))?

Well, you can write

pigeon% ruby -e 'p Integer(12.4)'  
12  
pigeon%

> And why is Integer capitalized? I know of  
> only four methods in the entire Ruby core  
> that start with capital letters.

and they do the same thing (a conversion) 🙂

Guy Decoux

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [24 June 2002 12:23 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/6 "2002-06-24T12:23:58Z")

</div>

> Integer(nil) == 0

&nbsp;&nbsp;nil.to\_i is defined

> Integer(false) #-\> exception

false.to\_i is not defined

> Array(foo) == foo.to\_a

&nbsp;&nbsp;Well, first ruby look if #to\_ary exist and call it otherwise it try to  
&nbsp;&nbsp;call #to\_a

> Integer(foo) != foo.to\_i  
> Float(foo) != foo.to\_f

it depend of foo

pigeon% ruby  
class A  
&nbsp;&nbsp;&nbsp;def to\_f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12.0  
&nbsp;&nbsp;&nbsp;end  
end

p Float(A.new)  
^D  
12.0  
pigeon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [24 June 2002 15:27 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/7 "2002-06-24T15:27:03Z")

</div>

Hi,

> I find this mildly strange:
> 
> Integer(‘’) #-\> exception
> 
> Integer(nil) == 0  
> Integer(false) #-\> exception
> 
> Integer(:foo) == 14145  
> Integer(‘foo’) #-\> exception
> 
> Is this ‘jus the way it is’, or is there a reason? I suppose symbols get  
> hashed, but then why not apply the same logic to strings as well?

Something that isn’t naturally convertable to an integer causes  
error. Symbols and nil are exception from historical reason, but  
maybe they will be error in the future.

> Why ?
> 
> Array(foo) == foo.to\_a  
> String(foo) == foo.to\_s  
> Integer(foo) != foo.to\_i  
> Float(foo) != foo.to\_f

Functional style methods are more strict than their OO style  
counterpart in general.

```
						matz.

```

> **···**
>
> In message “Re: No exceptions from String#to\_i” on 02/06/24, Nikodemus Siivola [tsiivola@cc.hut.fi](mailto:tsiivola@cc.hut.fi) writes:

---

<div class="post-metadata">

**Author:** ![Nikodemus\_Siivola](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nikodemus\_Siivola](https://rubytalk.org/u/Nikodemus_Siivola)\
**Post date:** [24 June 2002 12:54 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/8 "2002-06-24T12:54:42Z")

</div>

Thank you for the explanation of the mechanism. I am also wondering on the  
_rationale_ for differences between then four Kernel methods and various  
to\_X methods. The same applies to the difference between to\_str/to\_s and  
to\_ary/to\_a. Different tools for different jobs, but as to what those jobs  
might be is a bit murky for me.

Can someone enlighten me?

– Nikodemus

> **···**
>
> On Mon, 24 Jun 2002, ts wrote:
> 
> > nil.to\_i is defined

---

<div class="post-metadata">

**Author:** ![Florian\_Frank1](https://avatars.discourse-cdn.com/v4/letter/f/a88e57/32.png) [@Florian\_Frank1](https://rubytalk.org/u/Florian_Frank1)\
**Post date:** [24 June 2002 14:59 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/9 "2002-06-24T14:59:04Z")

</div>

There are exceptions to this rule:

Float(nil) =\> 0.0

object.c:  
case T\_NIL:  
return rb\_float\_new(0.0);

But nil.to\_f throws an exception because it isn’t defined.

Someone posted an RCR on Ruby Garden concerning this issue:

> **[WebMD - Better information. Better health.](https://www.webmd.com/?sid=241)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

This small patch adds the to\_f-method to NilClass:

Index: object.c

> **···**
>
> Am 2002-06-24 21:23:58 +0900 schrieb ts:
> 
> > > Integer(nil) == 0
> > 
> > nil.to\_i is defined
> > 
> > > Integer(false) #-\> exception
> > 
> > false.to\_i is not defined
> > 
> > > Array(foo) == foo.to\_a
> > 
> > Well, first ruby look if #to\_ary exist and call it otherwise it try to  
> > call #to\_a
> > 
> > > Integer(foo) != foo.to\_i  
> > > Float(foo) != foo.to\_f
> > 
> > it depend of foo
> 
> ===================================================================  
> RCS file: /src/ruby/object.c,v  
> retrieving revision 1.79  
> diff -u -r1.79 object.c  
> — object.c 2002/05/21 05:39:19 1.79  
> +++ object.c 2002/06/24 14:49:46  
> @@ -338,6 +338,13 @@  
> }
> 
> static VALUE  
> +nil\_to\_f(obj)
> 
> - VALUE obj;  
> +{
> - 
> 
> ```
> return rb_float_new(0.0);
> 
> ```
> 
> +}  
> +  
> +static VALUE  
> nil\_to\_s(obj)  
> VALUE obj;  
> {  
> @@ -1072,9 +1079,6 @@  
> case T\_STRING:  
> return rb\_float\_new(rb\_str\_to\_dbl(val, Qtrue));
> 
> - 
> 
> ```
> case T_NIL:
> 
> ```
> 
> - 
> 
> ```
> return rb_float_new(0.0);
> 
> ```
> 
> - 
> 
> ```
> default:
> return rb_convert_type(val, T_FLOAT, "Float", "to_f");
> 
> ```
> 
> }  
> @@ -1282,6 +1286,7 @@
> 
> rb\_cNilClass = rb\_define\_class(“NilClass”, rb\_cObject);  
> rb\_define\_method(rb\_cNilClass, “to\_i”, nil\_to\_i, 0);
> 
> - rb\_define\_method(rb\_cNilClass, “to\_f”, nil\_to\_f, 0);  
> rb\_define\_method(rb\_cNilClass, “to\_s”, nil\_to\_s, 0);  
> rb\_define\_method(rb\_cNilClass, “to\_a”, nil\_to\_a, 0);  
> rb\_define\_method(rb\_cNilClass, “inspect”, nil\_inspect, 0);
> 
> I don’t know if this breaks any existing code.
> 
> –  
> To assert that the earth revolves around the sun is as erroneous as to claim  
> that Jesus was not born of a virgin.  
> – Cardinal Bellarmine, “The trial of Galileo”, 1615

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [24 June 2002 17:11 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/10 "2002-06-24T17:11:07Z")

</div>

Unfortunately, it’s not possible to do this with user-defined data  
types. If I have a String-like class with a to\_i method that I want to  
return 0 on error, then I can do that, but then Integer() will also  
return 0 on error. If I want Integer() to raise an exception on error,  
then to\_i must also raise an exception on error.

It’s not good to have a standard library feature that user libraries  
cannot emulate.

Paul

> **···**
>
> On Tue, Jun 25, 2002 at 12:27:03AM +0900, Yukihiro Matsumoto wrote:
> 
> > Functional style methods are more strict than their OO style  
> > counterpart in general.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [24 June 2002 13:12 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/11 "2002-06-24T13:12:26Z")

</div>

> Thank you for the explanation of the mechanism. I am also wondering on the  
> \_rationale\_ for differences between then four Kernel methods and various  
> to\_X methods.

Look at [ruby-talk:1007]

&nbsp;&nbsp;&nbsp;[http://www.ruby-talk.org/1007](http://www.ruby-talk.org/1007)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The same applies to the difference between to\_str/to\_s and  
> to\_ary/to\_a. Different tools for different jobs, but as to what those jobs  
> might be is a bit murky for me.

[ruby-talk:23653]

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Nikodemus\_Siivola](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nikodemus\_Siivola](https://rubytalk.org/u/Nikodemus_Siivola)\
**Post date:** [24 June 2002 21:36 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/12 "2002-06-24T21:36:59Z")

</div>

Agreed. Could functional style call to\_ary, to\_int, to\_str and to\_flt  
instead?

– Nikodemus

> **···**
>
> On Tue, 25 Jun 2002, Paul Brannan wrote:
> 
> > It’s not good to have a standard library feature that user libraries  
> > cannot emulate.

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [25 June 2002 08:35 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/13 "2002-06-25T08:35:23Z")

</div>

Hi,

> **···**
>
> In message “Re: No exceptions from String#to\_i” on 02/06/25, Paul Brannan [pbrannan@atdesk.com](mailto:pbrannan@atdesk.com) writes:
> 
> > Unfortunately, it’s not possible to do this with user-defined data  
> > types. If I have a String-like class with a to\_i method that I want to  
> > return 0 on error, then I can do that, but then Integer() will also  
> > return 0 on error. If I want Integer() to raise an exception on error,  
> > then to\_i must also raise an exception on error.
> > 
> > It’s not good to have a standard library feature that user libraries  
> > cannot emulate.
> 
> How about Array(), Integer(), String() use to\_ary, to\_int, to\_str  
> respectively [ruby-talk:43014], and Float() raises error if to\_f  
> return NaN?
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Martin\_S\_Weber](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Martin\_S\_Weber](https://rubytalk.org/u/Martin_S_Weber)\
**Post date:** [24 June 2002 21:41 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/14 "2002-06-24T21:41:50Z")

</div>

Couldn’t we get type-bound generic functions instead of message passing style calls ?  
_blink_

-Martin (not 'serious)

> **···**
>
> On Tue, Jun 25, 2002 at 06:36:59AM +0900, Nikodemus Siivola wrote:
> 
> > On Tue, 25 Jun 2002, Paul Brannan wrote:
> > 
> > > It’s not good to have a standard library feature that user libraries  
> > > cannot emulate.
> > 
> > Agreed. Could functional style call to\_ary, to\_int, to\_str and to\_flt  
> > instead?

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [25 June 2002 07:39 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/15 "2002-06-25T07:39:38Z")

</div>

Hi,

> **···**
>
> In message “Re: No exceptions from String#to\_i” on 02/06/25, Nikodemus Siivola [tsiivola@cc.hut.fi](mailto:tsiivola@cc.hut.fi) writes:
> 
> > > It’s not good to have a standard library feature that user libraries  
> > > cannot emulate.
> > 
> > Agreed. Could functional style call to\_ary, to\_int, to\_str and to\_flt  
> > instead?
> 
> It seems a good idea. Let me think about it.  
> Will you submit RCR just for a record?
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Nikodemus\_Siivola](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nikodemus\_Siivola](https://rubytalk.org/u/Nikodemus_Siivola)\
**Post date:** [25 June 2002 09:02 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/16 "2002-06-25T09:02:34Z")

</div>

Sounds decent, but isn’t NaN technically a float? Also, since code that  
handles floats should also be able to handle NaNs, it might be simpler to  
just pass it along. Having it raise an error means that in some cases  
there will have to be handling for both the exception from Float() and  
handling for NaNs in general.

Also, why handle Float() differently? If because there is no to\_flt as of  
yet, then that holds for to\_int as well… 😉

– Nikodemus

> **···**
>
> On Tue, 25 Jun 2002, Yukihiro Matsumoto wrote:
> 
> > respectively [ruby-talk:43014], and Float() raises error if to\_f  
> > return NaN?

---

<div class="post-metadata">

**Author:** ![Nikodemus\_Siivola](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nikodemus\_Siivola](https://rubytalk.org/u/Nikodemus_Siivola)\
**Post date:** [25 June 2002 08:46 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/17 "2002-06-25T08:46:52Z")

</div>

Done.

Since it seems that core Integer, Float, String and Array are sort  
of core classes in Ruby, it might also be nice to redefine Object’s  
to\_s, to\_i, to\_f and to\_a to call the long form if it exists. This way the  
long form would give the short one for free.

Something like:

class Object  
def to\_s  
if respond\_to? :to\_str  
to\_str  
else  
inspect  
end  
end  
def to\_a  
if respond\_to? :to\_ary  
to\_ary  
else  
[self]  
end  
end  
def method\_missing( message, \*args )  
case message  
when :to\_i then to\_int if respond\_to? :to\_int  
when :to\_f then to\_flt if respond\_to? :to\_flt  
end  
end

– Nikodemus

> **···**
>
> On Tue, 25 Jun 2002, Yukihiro Matsumoto wrote:
> 
> > It seems a good idea. Let me think about it.  
> > Will you submit RCR just for a record?

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [25 June 2002 14:23 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/18 "2002-06-25T14:23:28Z")

</div>

Hi,

> > respectively [ruby-talk:43014], and Float() raises error if to\_f  
> > return NaN?
> 
> Sounds decent, but isn’t NaN technically a float? Also, since code that  
> handles floats should also be able to handle NaNs, it might be simpler to  
> just pass it along. Having it raise an error means that in some cases  
> there will have to be handling for both the exception from Float() and  
> handling for NaNs in general.

Good point. Hmm…

> Also, why handle Float() differently? If because there is no to\_flt as of  
> yet, then that holds for to\_int as well… 😉

Mostly because I didn’t see cases “to\_flt” is useful.

```
						matz.

```

> **···**
>
> In message “Re: No exceptions from String#to\_i” on 02/06/25, Nikodemus Siivola [tsiivola@cc.hut.fi](mailto:tsiivola@cc.hut.fi) writes:

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [25 June 2002 14:23 UTC](https://rubytalk.org/t/no-exceptions-from-string-to-i/372/19 "2002-06-25T14:23:26Z")

</div>

Hi,

> **···**
>
> In message “Re: No exceptions from String#to\_i” on 02/06/25, Nikodemus Siivola [tsiivola@cc.hut.fi](mailto:tsiivola@cc.hut.fi) writes:
> 
> > Since it seems that core Integer, Float, String and Array are sort  
> > of core classes in Ruby, it might also be nice to redefine Object’s  
> > to\_s, to\_i, to\_f and to\_a to call the long form if it exists. This way the  
> > long form would give the short one for free.
> 
> I don’t think it’s needed. It slows down the performance.
> 
> ```
> matz.
> 
> ```
