class Integer

Constants

ROMANS

class Integer is the pre-defined class of Integer numbers. what we are doing below is extending the class by adding a new method. to see the built in methods for class of integer type something like `5.methods.sort`

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