# What is happening here, Batman?

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.

1. 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?

2. And what’s the significance of the final “roman” before ‘end’?

3. 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!!

1. ‘D’ * 3 = repeat string ‘D’ three times = ‘DDD’
2. last calculated value returns from method automaticaly
1. basically it takes your 1999 and convert it to the Roman numerals,
see
further Roman numerals - Wikipedia
2. you can think of it as of ‘return roman’. ‘return’ keyword may be
(and
usually is, it’s a good practice) omitted if the value is returned at
the
end of method.
3. this is the logic of converting modern numerals to the Roman ones.
What
would you need to change it?

2012/8/29 incag neato [email protected]:

1. 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?

The first line sets the variable “roman” to an empty string. The
following lines append parts of the resulting roman numeral to the
same string.

– Matma R.