我正在做一个练习,我必须创建一个罗马到阿拉伯数字转换器。据我所知,下面的代码完全符合 kosher 标准,但是在运行测试时我不断收到错误消息。Ruby 认为第 37 行有一个未定义的方法或变量(在下面的注释中注明)。
我想知道我的 snytax 是关闭还是其他原因。建议?
class ArabicNumeral
def replace_troublesome_roman_numerals(letters)
tough_mappings = {"CM" => "DCCCC", "CD" => "CCCC", "XC" => "LXXXX", "XL" => "XXXX", "IX"=> "VIIII", "IV" => "IIII"}
tough_mappings.each { |roman, arabic| letters = letters.gsub(roman, arabic) }
letters
end
def convert_and_add(letters)
digits = { "M" => 1000, "CM" => 900, "D" => 500, "C" => 100, "XC" => 90, "L" => 50, "XL" => 40, "X" => 10, "IX" => 9, "V" => 5, "IV" => 4, "I" => 1}
letters = letters.split("")
letters.inject(0) do |sum, letter|
arabic = digits[letter]
sum += arabic
end
end
def self.convert(letters)
roman_string = replace_troublesome_roman_numerals(letters) ###LINE 37!
arabic_number = convert_and_add(roman_string)
arabic_number
end
end