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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cousin_roman/arabian.rb', line 37

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



30
31
32
33
34
35
# File 'lib/cousin_roman/arabian.rb', line 30

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



21
22
23
24
25
26
27
28
# File 'lib/cousin_roman/arabian.rb', line 21

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
17
18
19
# File 'lib/cousin_roman/arabian.rb', line 13

def to_roman!(arabian)
  if valid? arabian
    convert arabian
  else
    raise TypeError, 'not a valid roman number'
  end
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