# Convert Integers to Strings

**URL:** <https://www.ruby-forum.com/t/convert-integers-to-strings/156975>\
**Category:** Ruby\
**Created:** [January 5, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/convert-integers-to-strings/156975 "2009-01-05T23:42:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [January 5, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/convert-integers-to-strings/156975/1 "2009-01-05T23:42:35Z")

</div>

Howdy,

New to Ruby and looking for some help.  
I would like to prompt the user for input String of single [0 - 9 incl]  
digits e.g 57  
and convert these to word format and store as a single string e.g  
fiveseven

What way is best to go about doing this.

Should I create a hash of {1 =\> “one”…} and then iterate through the  
input, swapping digits that match a key with the key’s value.

Thanks a lot

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [January 5, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/convert-integers-to-strings/156975/2 "2009-01-05T23:57:39Z")

</div>

On Jan 5, 2009, at 14:43 , Gary C.topher wrote:

> Should I create a hash of {1 =\> “one”…} and then iterate through  
> the  
> input, swapping digits that match a key with the key’s value.

yes but… gsub is much easier

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [January 6, 2009, 12:00am UTC](https://www.ruby-forum.com/t/convert-integers-to-strings/156975/3 "2009-01-06T00:00:16Z")

</div>

Thanks mate

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [January 6, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/convert-integers-to-strings/156975/4 "2009-01-06T17:52:55Z")

</div>

Nice one Lars.

Peace

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [January 6, 2009, 11:55am UTC](https://www.ruby-forum.com/t/convert-integers-to-strings/156975/5 "2009-01-06T11:55:27Z")

</div>

- Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > =\> {“1”=\>“one”}
> > 
> > > > 111.to\_s.gsub(/\d/) { |n| numbers[n] }  
> > > > =\> “oneoneone”

I’d rather use an array than a hash in this case:

> > numbers = %w(zero one two three four five six seven eight nine)  
> > =\> [“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”,  
> > “eight”, “nine”]  
> > 2009.to\_s.gsub(/\d/) { |n| numbers[n.to\_i] }  
> > =\> “twozerozeronine”

–  
Lars H.

“If anyone disagrees with anything I say, I am quite prepared not only  
to  
retract it, but also to deny under oath that I ever said it.” -Tom  
Lehrer
