Convert Integers to Strings

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

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

Thanks mate

Nice one Lars.

Peace

=> {“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