Hello! Been working thru Chris P.'s book for noobies to ruby, and got
to the roman-numeral question. This one is new to me, given the material
thus far.
def old_roman_numeral num
roman = ‘’
roman = roman + ‘D’ * (num % 1000 / 500)
roman = roman + ‘C’ * (num % 500 / 100)
roman = roman + ‘L’ * (num % 100 / 50)
roman = roman + ‘X’ * (num % 50 / 10)
roman = roman + ‘V’ * (num % 10 / 5)
roman = roman + ‘I’ * (num % 5 / 1)
roman
end
puts(old_roman_numeral(1999))
I have broken this code down into pieces to understand what’s happening,
but still a little fuzzy.
I have a decent grasp on the modulus operator and multiplying strings.
-
But, when I pass the integer (1999 in this case) thru, what exactly
is happening? Is it simply “picked up” by roman = ‘’ and run thru? -
And what’s the significance of the final “roman” before ‘end’?
-
Also, how can one deduce the necessity to divide again after applying
the modulus op? (E.g. 500, 100, 50, 10, etc.) I feel like I’ve opened a
calculator and I just don’t ‘get’ how it was engineered.
I appreciate your effort in helping me understand!!