1

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.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
balee1991
  • 31
  • 5

2 Answers2

2

There's 2 errors, first one:

Arabic_Numberals = Hash[Roman_Numerals.to_a.reverse.reverse]

This code does literally nothing. What you want to do here is swap the key/values, so you can use Hash#invert:

Arabic_Numberals = Roman_Numerals.invert

The other one is when you compare the input:

if input == arabic

It will compare '5' == 5, which is false. You need to convert the input to integer before comparing:

if input.to_i == arabic

I hope it helps!

Leonardo Prado
  • 598
  • 5
  • 12
  • I have tried the input.to_i before I believe... but maybe the .invert part will fix my issue. I've tried multiple scenarios, but let me try this later at home tonight and I will give tell you my results. – balee1991 Apr 06 '17 at 13:13
  • It's because the `.to_i` wouldn't work without the corrected hash (it would compare `5 == 'V'`) – Leonardo Prado Apr 06 '17 at 17:54
  • Using .invert and converting input.to_i at the argument worked. Thank you. – balee1991 Apr 07 '17 at 02:25
  • Now that I can convert my inputs into the cases. I have to input any number and convert. Instead of doing whats only in the list. – balee1991 Apr 07 '17 at 02:25
1

I have removed a lot of comments and extra conditions from your code. Please see if the following version of the code is what you are looking for:

ROMAN_NUMBER_MAP = {
  "M" => 1000,
  "D" => 500,
  "C" => 100,
  "L" => 50,
  "X" => 10,
  "V" => 5,
  "I" => 1,
}

input = gets.chomp.upcase

def numeric?
  !!Float(self) rescue false
end

if input.numeric?
  puts "Answer: The Number '#{input}' => #{ROMAN_NUMBER_MAP.key(input.to_i)}"
else
  puts "Answer: The Roman Numeral '#{input}' => #{ROMAN_NUMBER_MAP[input]}."
end
Jagdeep Singh
  • 4,880
  • 2
  • 17
  • 22
  • The if statement works, but the else statement is failing. Although I can see how that can shorten up the code quite a bit. Also, the method numeric? is missing a "public" before it. It won't work until I add that there. – balee1991 Apr 07 '17 at 02:28