class Integer
Constants
Public Instance Methods
to_roman()
click to toggle source
# File lib/hack01/roman_numerals.rb, line 24 def to_roman number = self # self is like `this` in javascript. in this example if we call this method thusly `5.to_roman` then self is 5 output = "" # long way. would need lots of if / else statements. # if number == 1 # output = "I" # elsif number == 2 # output = "II" # else # output = "III" # end # clever ruby way - eaching through the hash defined above. ROMANS.each do |key, value| # output = output + key * ( number / value ) output << key * ( number / value ) number = number % value end output end