I am converting roman numerals to a number and vice versa by using a case
statement. My case takes in a true or false by checking if I input a string or an integer. If I input 5 I should get out V and if I input M I should get out 1000.
I am able to get my "False" case to work correctly. But I CANNOT get my "True" case to work.
I reversed my Roman_Numerals
to be a reverse hash called Arabic_Numerals
. I don't see why my true
case does not work as it did the opposite.
Roman_Numerals = {
"M" => 1000,
"D" => 500,
"C" => 100,
"L" => 50,
"X" => 10,
"V" => 5,
"I" => 1,
}
#Reverses the Roman Numerals and Arabic Numbers around in the Hash
#to look like 1000 => "M".
Arabic_Numberals = Hash[Roman_Numerals.to_a.reverse.reverse]
input = gets.chomp.upcase
def numeric?
Float(self) != nil rescue false
end
true_false = input.numeric?
#Looks for true or false from true_false variable. Then goes through
#the case to convert a roman numeral or a number.
case true_false
when false
#Converts a Roman Numeral to a Number
Roman_Numerals.each do |roman, value|
puts "#{roman}:#{value}"
if roman == input
puts "Answer: The Roman Numeral '#{input}' => #{value}."
break
else
next
end
end
#Converts a Number to a Roman Numeral
when true
Arabic_Numberals.each do |arabic, letter|
puts "#{letter}:#{arabic}"
puts "#{input}"
if input == arabic
puts "Answer: The Number '#{input}' => #{letter}"
break
else
puts "Why isn't this code working?"
next
end
end
end
Please advice on why my false
case does not work. I am not sure why arabic == input
does not work.