我有一个下面这样的哈希表:
a_hash = {
"1" => "one",
"2" => "two",
"3" => "three",
"4" => "four",
"5" => "five",
"6" => "six",
"7" => "seven",
"8" => "eight",
"9" => "nine",
"10" => "ten",
"11" => "eleven",
"12" => "twelve",
"13" => "thirteen",
"14" => "fourteen",
"15" => "fifteen",
"16" => "sixteen",
"17" => "seventeen",
"18" => "eighteen",
"19" => "nineteen",
"20" => "twenty",
"30" => "thirty",
"40" => "forty",
"50" => "fifty",
"60" => "sixty",
"70" => "seventy",
"80" => "eighty",
"90" => "ninety",
"00" => "hundred", #not sure this is right
"000" => "thousand" #not sure this is right
}
假设我的字符串输入是"99100"。 假设我想要的字符串输出是"九万九千一百"。 如果不想一个一个地键入每个键/值,该如何使用上述哈希表?我在考虑将我的字符串拆分成一个数组中的每个字符....然后对于该数组中的每个数字返回我的值?还有其他策略应该考虑吗?这是我目前的进展:
puts "test the hash! Type a number hit enter"
test_variable = gets.to_s.chomp
puts a_hash[test_variable]
请发一些代码让我测试。谢谢!