# Type conversion

**URL:** <https://rubytalk.org/t/type-conversion/39196>\
**Category:** ruby-talk\
**Created:** [17 July 2007 20:11 UTC](https://rubytalk.org/t/type-conversion/39196 "2007-07-17T20:11:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shandy\_Nantz](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@Shandy\_Nantz](https://rubytalk.org/u/Shandy_Nantz)\
**Post date:** [17 July 2007 20:11 UTC](https://rubytalk.org/t/type-conversion/39196/1 "2007-07-17T20:11:43Z")

</div>

So there is an to\_s for strings and a to\_i for integers. However, if I  
have an array of integers and I want to grab one, I get back and ASCII  
character. For example, when I grab a 1 from the array it returns a 49 -  
but I wanted the one! How do I convert it back to its integer  
want-to-be? Thanks,

~S

P.S. I imagine this is probably an easy solution but, I am a ruby  
newbie.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Britt3](https://avatars.discourse-cdn.com/v4/letter/j/b782af/32.png) [@James\_Britt3](https://rubytalk.org/u/James_Britt3)\
**Post date:** [17 July 2007 20:31 UTC](https://rubytalk.org/t/type-conversion/39196/2 "2007-07-17T20:31:39Z")

</div>

Shandy Nantz wrote:

> So there is an to\_s for strings and a to\_i for integers. However, if I  
> have an array of integers and I want to grab one, I get back and ASCII  
> character.

Are you sure you have an array of integers?

Show some code.

> For example, when I grab a 1 from the array it returns a 49 -  
> but I wanted the one! How do I convert it back to its integer  
> want-to-be? Thanks,

This works for me:

# Create int array, 11 to 19  
# the \* expands the range into an array  
a = \*11..19

# print it to see what we have  
p a

# See what we have at the first location  
p a[0] # I get 11

> **···**
>
> --  
> James Britt
> 
> "Take eloquence and wring its neck."  
> &nbsp;&nbsp;- Paul Verlaine

---

<div class="post-metadata">

**Author:** ![Ian\_Whitlock](https://avatars.discourse-cdn.com/v4/letter/i/e47c2d/32.png) [@Ian\_Whitlock](https://rubytalk.org/u/Ian_Whitlock)\
**Post date:** [17 July 2007 22:05 UTC](https://rubytalk.org/t/type-conversion/39196/3 "2007-07-17T22:05:51Z")

</div>

Shandy Nantz wrote:

> So there is an to\_s for strings and a to\_i for integers. However, if I  
> have an array of integers and I want to grab one, I get back and ASCII  
> character. For example, when I grab a 1 from the array it returns a 49 -  
> but I wanted the one! How do I convert it back to its integer  
> want-to-be? Thanks,
> 
> ~S
> 
> P.S. I imagine this is probably an easy solution but, I am a ruby  
> newbie.

Shandy, Perhaps this helps. Note the difference between 004 and 006

irb(main):003:0\> i = "1234"  
=\> "1234"  
irb(main):004:0\> i[0]  
=\> 49  
irb(main):005:0\> i[0].class  
=\> Fixnum  
irb(main):006:0\> i[0,1]  
=\> "1"  
irb(main):007:0\> i[0,1].class  
=\> String  
irb(main):008:0\> i[0,1].to\_i  
=\> 1  
irb(main):009:0\> i[0,1].to\_i.class  
=\> Fixnum

Good luck from another newbie.  
Ian

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [17 July 2007 22:42 UTC](https://rubytalk.org/t/type-conversion/39196/4 "2007-07-17T22:42:20Z")

</div>

arr = "1234"

Will only give you a string.  
You need :  
arr = Array.new  
arr \<\< 1  
arr \<\< 2  
...  
OR  
arr = [1,2,3,4,]

At the command line, use ri:  
ri Array  
ri '='  
ri 'Array#='  
ri 'String#='

> **···**
>
> On Jul 17, 2007, at 5:05 PM, Ian Whitlock wrote:
> 
> > Shandy Nantz wrote:
> > 
> > > So there is an to\_s for strings and a to\_i for integers. However, if I  
> > > have an array of integers and I want to grab one, I get back and ASCII  
> > > character. For example, when I grab a 1 from the array it returns a 49 -  
> > > but I wanted the one! How do I convert it back to its integer  
> > > want-to-be? Thanks,
> > > 
> > > ~S
> > > 
> > > P.S. I imagine this is probably an easy solution but, I am a ruby  
> > > newbie.
> > 
> > Shandy, Perhaps this helps. Note the difference between 004 and 006
> > 
> > irb(main):003:0\> i = "1234"  
> > =\> "1234"  
> > irb(main):004:0\> i[0]  
> > =\> 49  
> > irb(main):005:0\> i[0].class  
> > =\> Fixnum  
> > irb(main):006:0\> i[0,1]  
> > =\> "1"  
> > irb(main):007:0\> i[0,1].class  
> > =\> String  
> > irb(main):008:0\> i[0,1].to\_i  
> > =\> 1  
> > irb(main):009:0\> i[0,1].to\_i.class  
> > =\> Fixnum
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Shandy\_Nantz](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@Shandy\_Nantz](https://rubytalk.org/u/Shandy_Nantz)\
**Post date:** [18 July 2007 14:51 UTC](https://rubytalk.org/t/type-conversion/39196/5 "2007-07-18T14:51:41Z")

</div>

Sorry about not giving code. I did finally get it to work, but here is  
what I had to do:

card\_num = "123456789"  
c = 0

char = card\_num[i..i].to\_i

before I had:

char = card\_num[i].to\_i

All I wanted to do was to pick out a single character (a digit) from  
that string and perform some calculations on it. But by just saying [i]  
I got a 57 value when I wanted the 9 value. When I said [i..i] I got the  
9 value. Thanks for all your help,

~S

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [18 July 2007 15:01 UTC](https://rubytalk.org/t/type-conversion/39196/6 "2007-07-18T15:01:03Z")

</div>

Shandy Nantz wrote:

> Sorry about not giving code. I did finally get it to work, but here is what I had to do:
> 
> card\_num = "123456789"  
> c = 0
> 
> char = card\_num[i..i].to\_i
> 
> before I had:
> 
> char = card\_num[i].to\_i
> 
> All I wanted to do was to pick out a single character (a digit) from that string and perform some calculations on it. But by just saying [i] I got a 57 value when I wanted the 9 value. When I said [i..i] I got the 9 value. Thanks for all your help,
> 
> ~S

Card\_num[i] is returning the character value at position i.

card\_num[i].chr will return the character at position i

card\_num[i].chr.to\_i will return the integer value of the character

card\_num[i].type =\> Fixnum

card\_num[i..i].type =\> String and is why card\_num[i..i].to\_i worked

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [18 July 2007 15:07 UTC](https://rubytalk.org/t/type-conversion/39196/7 "2007-07-18T15:07:20Z")

</div>

It's a bit late, I guess, but did you consider something like:

&nbsp;&nbsp;&nbsp;&nbsp;"123456789"[-1] - ?0 # =\> 9

Regards, Morton

> **···**
>
> On Jul 18, 2007, at 10:51 AM, Shandy Nantz wrote:
> 
> > Sorry about not giving code. I did finally get it to work, but here is  
> > what I had to do:
> > 
> > card\_num = "123456789"  
> > c = 0
> > 
> > char = card\_num[i..i].to\_i
> > 
> > before I had:
> > 
> > char = card\_num[i].to\_i
> > 
> > All I wanted to do was to pick out a single character (a digit) from  
> > that string and perform some calculations on it. But by just saying [i]  
> > I got a 57 value when I wanted the 9 value. When I said [i..i] I got the  
> > 9 value. Thanks for all your help,
