ruby - Roman numeral conversion in different approach -


i converting roman numerals number , vice versa using case statement. case takes in true or false checking if input string or integer. if input 5 should out v , if input m should out 1000. able "false" case work correctly. cannot "true" case work.

i reversed roman_numerals reverse hash called arabic_numerals. don't see why true case not work did opposite.

roman_numerals = {   "m" => 1000,   "d" => 500,   "c" => 100,   "l" => 50,   "x" => 10,   "v" => 5,   "i" => 1, }  #reverses roman numerals , arabic numbers around in hash #to 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 true or false true_false variable. goes through  #the case convert roman numeral or number. case true_false when false   #converts roman numeral number   roman_numerals.each |roman, value|     puts "#{roman}:#{value}"     if roman == input       puts "answer: roman numeral '#{input}' => #{value}."       break     else       next     end   end   #converts number roman numeral when true   arabic_numberals.each |arabic, letter|   puts "#{letter}:#{arabic}"   puts "#{input}"   if input == arabic     puts "answer: number '#{input}' => #{letter}"     break   else     puts "why isn't code working?"     next   end end end 

please advice on why false case not work. not sure why arabic == input not work.

there's 2 errors, first one:

arabic_numberals = hash[roman_numerals.to_a.reverse.reverse] 

this code literally nothing. want here swap key/values, can use hash#invert:

arabic_numberals = roman_numerals.invert 

the other 1 when compare input:

if input == arabic 

it compare '5' == 5, false. need convert input integer before comparing:

if input.to_i == arabic 

i hope helps! 🙂


Comments