PDA

View Full Version : Ruby - Change single digit integers to word strings



rlzyoner
January 6th, 2009, 12:06 AM
Howdy,

What would be the best way, in Ruby, to go about changing a string like 1234 to onetwothreefour.

Should I create a hash that sets {1 => "one"...} ans so on ?

Thanks

Caduceus
January 6th, 2009, 04:58 PM
Knowing Ruby there will be a method already built in to do this, have a look through http://ruby-doc.org/core/ or http://ruby-doc.org/stdlib/

rlzyoner
January 6th, 2009, 06:18 PM
Got.

Just incase anyone needs it

>> 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"