Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 55 lines (45 sloc) 0.892 kb
ce96f05 @schneems ping
schneems authored
1
dcd753a @joshuaflanagan initial
authored
2 module RomanNumeral
ce96f05 @schneems ping
schneems authored
3 ORDER = { 1000 => "M",
13b2577 @joshuaflanagan done
authored
4 900 => "CM",
ce96f05 @schneems ping
schneems authored
5 500 => "D",
6 100 => "C",
13b2577 @joshuaflanagan done
authored
7 90 => "XC",
ce96f05 @schneems ping
schneems authored
8 50 => "L",
9 10 => "X",
13b2577 @joshuaflanagan done
authored
10 9 => "IX",
ce96f05 @schneems ping
schneems authored
11 5 => "V",
13b2577 @joshuaflanagan done
authored
12 4 => "IV",
ce96f05 @schneems ping
schneems authored
13 1 => "I"}
14
15
dcd753a @joshuaflanagan initial
authored
16 def to_roman
17 RomanNumeral.convert_numeric_to_roman self
18 end
19
20 def self.convert_numeric_to_roman(number)
ce96f05 @schneems ping
schneems authored
21 output = ""
22 ORDER.each do |key,value|
23 # puts "key: #{key}: value: #{value}"
13b2577 @joshuaflanagan done
authored
24 times = number / key
ce96f05 @schneems ping
schneems authored
25 while number >= key
26 number = number - key
27 output = output + value
28 end
29 end
30 output
dcd753a @joshuaflanagan initial
authored
31 end
32 end
33
34 class Numeric
35 include RomanNumeral
36 end
13b2577 @joshuaflanagan done
authored
37
38 class RomanNumeralString
39 attr_accessor :string
40
41 def initialize(roman)
42 self.string = roman
43 end
44
45 def to_i
46 0
47 end
48 end
49
50 class String
51 def to_roman
52 RomanNumeralString.new(self).to_i
53 end
54 end
Something went wrong with that request. Please try again.