Reputation: 1
I am creating a method that converts roman numbers to integers. I have written this:
def roman_to_integer(roman)
roman = roman.upcase
num_I = roman.count("I").to_i
num_V = roman.count("V").to_i
num_X = roman.count("X").to_i
num_L = roman.count("L").to_i
num_C = roman.count("C").to_i
num_D = roman.count("D").to_i
num_M = roman.count("M").to_i
rom_num = { "I" => 1,
"V" => 5,
"X" => 10,
"L" => 50,
"C" => 100,
"D" => 500,
"M" => 1000 }
while roman
if roman[0] == "M"
integer = num_M * rom_num["M"] + num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "D"
integer = num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "C"
integer = num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "L"
integer = num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "X"
integer = num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "V"
integer = num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "I"
integer = num_I * rom_num["I"]
end
end
return integer
end
puts "Tell me any number in Roman System and I will convert it to the Arabic Number!"
number = gets.chomp
puts "Here is your number:"
puts(roman_to_integer(number))
When it comes to the final line, where it should print out the result, I just get the empty input line in the command line. I don't understand why it doesn't work and need some help in finding where the code is wrong.
Also if I use
puts number.roman_to_integer
as a last line, I get
private method "roman_to_integer" called on "":String (NoMethodError)
Any help would be appreciated.
Upvotes: 0
Views: 166
Reputation: 37409
Your method does not return since it is stuck in the while roman
block - since you don't change the roman
variable, it will always be truthy, and the block will continue to run over and over again, never returning a value:
while roman
#... runs forever
end
Regardless of that, your method doesn't seem to do what you think it does ('IX'
will return 1?)
Upvotes: 2
Reputation: 9497
Remove your while
loop. I don't think it adds anything to the code. I've also improved formatting and removed the to_s
methods which were superfluous.
def roman_to_integer(roman)
roman = roman.upcase
# Counts the occurrences of each letter from the input
num_I = roman.count("I")
num_V = roman.count("V")
num_X = roman.count("X")
num_L = roman.count("L")
num_C = roman.count("C")
num_D = roman.count("D")
num_M = roman.count("M")
# Assigns values to every Roman letter
rom_num = { "I" => 1,
"V" => 5,
"X" => 10,
"L" => 50,
"C" => 100,
"D" => 500,
"M" => 1000 }
# Multiplies the value of the letter by number of occurrences
if roman[0] == "M"
integer = num_M * rom_num["M"] + num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "D"
integer = num_D * rom_num["D"] + num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "C"
integer = num_C * rom_num["C"] + num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "L"
integer = num_L * rom_num["L"] + num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "X"
integer = num_X * rom_num["X"] + num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "V"
integer = num_V * rom_num["V"] + num_I * rom_num["I"]
elsif roman[0] == "I"
integer = num_I * rom_num["I"]
end
return integer
end
puts "Tell me any number in Roman System and I will convert it to the Arabic Number!"
number = gets.chomp
puts "Here is your number:"
puts roman_to_integer(number)
Also to make puts number.roman_to_integer
work you would have to add your method to the String class, which you shouldn't do by the way.
Upvotes: 0