I think my question is more about general syntax, than more about
defining/creating methods. In doing one of the excerises in Chris
Pine’s Learning to Program for Chapter 9, Creating Own Methods, one is
to create roman number converter. Below is the program. I added the
roman number converter
I = 1, V = 5, X = 10, L = 50, C =100, D = 500, M = 1000
puts ‘pick a number to convert’
num = gets.to_i
def roman num
roman = ‘’ #assuming creating roman variable that’s empty
roman = roman + ‘M’ * (num/1000) #adding Ms to roman variable
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 # don’t understand purpose of line
puts roman num
I think I get the defining method. I just not clear on what’s inside.
Specially, the “roman = ‘’” and the other lines “roman = roman + …” I
think the prior is creating a variable roman that’s empty. The later is
stating to add how ever many roman number characters is created by
dividing num by whatever; like a push for arrays.
I don’t know what the “roman” at the end of the method does. When
removed, I get the same results.
Thanks for the help in advance.