Module: SleepingKingStudios::Tools::IntegerTools

Extended by:
IntegerTools
Included in:
IntegerTools
Defined in:
lib/sleeping_king_studios/tools/integer_tools.rb

Overview

Tools for working with integers.

Constant Summary collapse

ROMANIZE_MIN =

Minimum integer value that can be converted to a roman numeral.

1
ROMANIZE_MAX =

Maximum integer value that can be converted to a roman numeral.

4999

Instance Method Summary collapse

Instance Method Details

#count_digits(integer, base: 10) ⇒ Integer

Returns the number of digits in the given integer when represented in the specified base. Ignores minus sign for negative numbers.

Examples:

With a positive number.

IntegerTools.count_digits(31)
#=> 2

With a negative number.

IntegerTools.count_digits(-141)
#=> 3

With a binary number.

IntegerTools.count_digits(189, :base => 2)
#=> 8

With a hexadecimal number.

IntegerTools.count_digits(16724838, :base => 16)
#=> 6

Parameters:

  • integer (Integer)

    The integer to analyze.

  • base (Integer) (defaults to: 10)

    The numeric base to represent the integer in. Defaults to 10.

Returns:

  • (Integer)

    The number of digits.



40
41
42
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 40

def count_digits integer, base: 10
  digits(integer.abs, :base => base).count
end

#digits(integer, base: 10) ⇒ Array<String>

Decomposes the given integer into its digits when represented in the given base.

Examples:

With a number in base 10.

IntegerTools.digits(15926)
#=> ['1', '5', '9', '2', '6']

With a binary number.

IntegerTools.digits(189, :base => 2)
#=> ['1', '0', '1', '1', '1', '1', '0', '1']

With a hexadecimal number.

IntegerTools.digits(16724838)
#=> ['f', 'f', '3', '3', '6', '6']

Parameters:

  • integer (Integer)

    The integer to decompose.

  • base (Integer) (defaults to: 10)

    The numeric base to represent the integer in. Defaults to 10.

Returns:

  • (Array<String>)

    The digits of the decomposed integer, represented as a bigendian array of strings.



65
66
67
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 65

def digits integer, base: 10
  integer.to_s(base).split('')
end

#integer?(int) ⇒ Boolean

Returns true if the object is an Integer.

Parameters:

  • int (Object)

    The object to test.

Returns:

  • (Boolean)

    True if the object is an Integer, otherwise false.



74
75
76
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 74

def integer? int
  Integer === int
end

#pluralize(count, single, plural) ⇒ String

Returns the singular or the plural value, depending on the provided item count.

Examples:

"There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
#=> 'There are four lights!'

Parameters:

  • count (Integer)

    The number of items.

  • single (String)

    The singular form of the word or phrase.

  • plural (String)

    The plural form of the word or phrase.

Returns:

  • (String)

    The single form if count == 1; otherwise the plural form.



91
92
93
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 91

def pluralize count, single, plural
  1 == count ? single : plural
end

#romanize(integer, additive: false) ⇒ String

Represents an integer between 1 and 4999 (inclusive) as a Roman numeral.

Examples:

IntegerTools.romanize(4) #=> 'IV'
IntegerTools.romanize(18) #=> 'XVIII'
IntegerTools.romanize(499) #=> 'CDXCIX'

Parameters:

  • integer (Integer)

    The integer to convert.

  • additive (Boolean) (defaults to: false)

    If true, then uses only additive Roman numerals (e.g. four will be converted to IIII instead of IV, and nine will be converted to VIIII instead of IX). Defaults to false.

Returns:

  • (String)

    The representation of the integer as a Roman numeral.

Raises:

  • (RangeError)

    If the integer is less than 1 or greater than 4999.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 114

def romanize integer, additive: false
  # Validate input value.
  unless (ROMANIZE_MIN..ROMANIZE_MAX).include? integer
    raise RangeError.new "integer to romanize must be within range #{ROMANIZE_MIN} to #{ROMANIZE_MAX}"
  end # unless

  # Define conversion rules.
  rules = [
    '',
    '%one',
    '%one%one',
    '%one%one%one',
    additive ? '%one%one%one%one' : '%one%five',
    '%five',
    '%five%one',
    '%five%one%one',
    '%five%one%one%one',
    additive ? '%five%one%one%one%one' : '%one%ten',
    '%ten'
  ] # end array

  # Define numeral values.
  numerals = [
    %w(I V X),
    %w(X L C),
    %w(C D M),
    ['M', 'MMM', '']
  ] # end array numerals

  # Generate string representation.
  digits(integer).reverse.map.with_index do |digit, index|
    rules[digit.to_i]
      .gsub('%one',  numerals[index][0])
      .gsub('%five', numerals[index][1])
      .gsub('%ten',  numerals[index][2])
  end.reverse.join ''
end