# To\_i vs. to\_int

**URL:** <https://www.ruby-forum.com/t/to-i-vs-to-int/100505>\
**Category:** Ruby\
**Created:** [July 29, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505 "2006-07-29T18:01:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/1 "2006-07-29T18:01:16Z")

</div>

Hello,

I have a class which instances can be represented as integers. What is  
the difference between the to\_i and to\_int methods, and when is which  
form needed ?

Ico

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/2 "2006-07-29T18:46:16Z")

</div>

Ico ha scritto:

> Hello,
> 
> I have a class which instances can be represented as integers. What is  
> the difference between the to\_i and to\_int methods, and when is which  
> form needed ?

short names (to\_x) are for common programmer use.  
Long names (to\_xyz) are there so that ruby can do some implicit  
conversions between things that can basically be threated as xyz.

So in your case it could be meaningful to define one and alias the other  
to it.

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/3 "2006-07-29T18:59:21Z")

</div>

Hi –

On Sun, 30 Jul 2006, gabriele renzi wrote:

> So in your case it could be meaningful to define one and alias the other to  
> it.

What’s a use case for to\_int? I was thinking of this classic to\_str  
example:

class S  
def to\_str  
“some text”  
end  
end

puts "Here is " + S.new # Here is some text

But the to\_int equivalent doesn’t work:

class S  
def to\_int  
2  
end  
end

puts 2 + S.new # TypeError: S can’t be coerced into Fixnum

Same thing with:

puts Integer(2) + S.new

I’m stuck trying to find a case where to\_int does what to\_str does –  
but maybe it doesn’t.

David

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/4 "2006-07-29T19:09:16Z")

</div>

> > > > > “d” == dblack [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

d\> class S  
d\> def to\_int  
d\> 2  
d\> end  
d\> end

d\> puts 2 + S.new # TypeError: S can’t be coerced into Fixnum

```
  puts 2 | S.new

```

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/5 "2006-07-29T19:15:31Z")

</div>

Hi –

On Sun, 30 Jul 2006, ts wrote:

> ```
> puts 2 | S.new
> 
> ```

Interesting. What’s the rationale for not doing the conversion for +  
and - and other operator/methods?

David

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/6 "2006-07-29T19:32:15Z")

</div>

On Jul 29, 2006, at 12:58 PM, removed\_email\_address@domain.invalid wrote:

> Same thing with:
> 
> puts Integer(2) + S.new
> 
> I’m stuck trying to find a case where to\_int does what to\_str does –  
> but maybe it doesn’t.

However,

```
puts 2 + Integer(S.new) => 4

```

uses your S#to\_int

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/7 "2006-07-29T22:52:36Z")

</div>

removed\_email\_address@domain.invalid ha scritto:

> > ```
> > puts 2 | S.new
> > 
> > ```

also, Arrays seem to like it:

> > a=Object.new  
> > =\> #Object:0x2c103a0
> 
> > def a.to\_int() 2 end  
> > =\> nil
> 
> > [0,1,3][a]  
> > =\> 3
> 
> > Array.new(a){ 0 }  
> > =\> [0, 0]

> Interesting. What’s the rationale for not doing the conversion for +  
> and - and other operator/methods?

I’d like to know the same 🙂

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 30, 2006, 10:36am UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/8 "2006-07-30T10:36:56Z")

</div>

We actually gabbed on this topic a bit recently. See the second half  
of the “Symbols are your friends” thread from a couple of days ago.

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 29, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/9 "2006-07-29T19:42:23Z")

</div>

Hi –

On Sun, 30 Jul 2006, Morton G. wrote:

> > puts 2 + S.new # TypeError: S can’t be coerced into Fixnum  
> > puts 2 + Integer(S.new) =\> 4
> 
> uses your S#to\_int

True, but it works the same with to\_i, as opposed to the string one  
where to\_s and to\_str work differently.

David

---

<div class="post-metadata">

**Author:** ![Ico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/91b2a8/32.png) [@Ico](https://www.ruby-forum.com/u/Ico)\
**Post date:** [July 30, 2006, 10:52am UTC](https://www.ruby-forum.com/t/to-i-vs-to-int/100505/10 "2006-07-30T10:52:20Z")

</div>

Aleks Kissinger ha scritto:

> We actually gabbed on this topic a bit recently. See the second half  
> of the “Symbols are your friends” thread from a couple of days ago.

Oh, I missed this, thank you
