Skip to content

Instantly share code, notes, and snippets.

@Scoutski
Created September 1, 2015 23:52
Show Gist options
  • Save Scoutski/e60895f4c6b9688da5fa to your computer and use it in GitHub Desktop.
Save Scoutski/e60895f4c6b9688da5fa to your computer and use it in GitHub Desktop.
Roman Numerals Solution
require 'pry'
class RomanNumerals
def initialize num
to_roman num
end
def to_roman num
result = ""
roman_mappings.each do |arabic, roman|
while num >= arabic
result << roman
num -= arabic
end
end
puts result
result
end
def roman_mappings
{
1000 => 'M',
900 => 'CM',
500 => 'D',
400 => 'CD',
100 => 'C',
90 => 'XC',
50 => 'L',
40 => 'XL',
10 => 'X',
9 => 'IX',
5 => 'V',
4 => 'IV',
1 => 'I',
}
end
end
roman = RomanNumerals.new 2900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment