Though there isn’t an error with the code per se, the example code in the back does not seem to fully solve the problem. It asks to make sure the string is a valid roman numeral. It checks to make sure each literal is a valid roman numeral, but it doesn’t check if the overall number is valid. For instance, the code provided will give you a value for IIIX even though that isn’t a valid roman numeral.
I think that putting in another piece of logic after the val checking would be helpful. For instance: if val < prev then, if val = the_character_before_it then puts ‘This is not a valid roman numeral.’ This would pick up on any situations, such as those mentioned above. Otherwise, X will continue to be the value for val, and you’ll just get a bunch of negative 1’s.
Just a thought.