Module: CousinRoman::Arabian

Extended by:
Arabian
Included in:
Arabian
Defined in:
lib/cousin_roman/arabian.rb

Instance Method Summary collapse

Instance Method Details

#build_pow(number, pow) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cousin_roman/arabian.rb', line 34

def build_pow(number, pow)
  literals = CousinRoman.literals_for_pow pow

  case number
  when 1..3 then literals[:one]*number
  when 4 then literals[:subtractives][4]
  when 5..8 then literals[:five] + (literals[:one]*(number - 5))
  when 9 then literals[:subtractives][9]
  else ""
  end
end

#build_roman(thousands, hundreds, tens, ones) ⇒ Object



27
28
29
30
31
32
# File 'lib/cousin_roman/arabian.rb', line 27

def build_roman(thousands, hundreds, tens, ones)
  build_pow(thousands, 3) +
  build_pow(hundreds, 2) +
  build_pow(tens, 1) +
  build_pow(ones, 0)
end

#convert(number) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/cousin_roman/arabian.rb', line 18

def convert(number)
  thousands = number / 1000
  hundreds = number / 100 % 10
  tens = number / 10 % 10
  ones = number % 10

  build_roman(thousands, hundreds, tens, ones).upcase
end

#to_roman(arabian) ⇒ Object



9
10
11
# File 'lib/cousin_roman/arabian.rb', line 9

def to_roman(arabian)
  convert arabian if valid? arabian
end

#to_roman!(arabian) ⇒ Object



13
14
15
16
# File 'lib/cousin_roman/arabian.rb', line 13

def to_roman!(arabian)
   valid? arabian or raise TypeError, 'not a valid roman number'
   convert arabian
end

#valid?(number) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/cousin_roman/arabian.rb', line 5

def valid?(number)
  number.is_a? Integer and number >= 1 and number <= 3999
end