Class: Integer

Inherits:
Object show all
Defined in:
lib/kitchen/patches/integer.rb

Overview

Monkey patches for Integer

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.roman_numeralsObject

Returns the value of attribute roman_numerals.



20
21
22
# File 'lib/kitchen/patches/integer.rb', line 20

def roman_numerals
  @roman_numerals
end

Instance Method Details

#to_format(format) ⇒ String

Formats as different types of integers, including roman numerals.

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kitchen/patches/integer.rb', line 27

def to_format(format)
  case format
  when :arabic
    to_s
  when :roman
    raise 'Unknown conversion to Roman numerals' if self > self.class.roman_numerals.keys.first

    to_roman
  else
    raise 'Unknown integer format'
  end
end

#to_romanObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kitchen/patches/integer.rb', line 40

def to_roman
  return 0 if zero?

  roman = ''
  integer = self
  self.class.roman_numerals.each do |number, letter|
    until integer < number
      roman += letter
      integer -= number
    end
  end
  roman
end