Class: SleepingKingStudios::Tools::IntegerTools

Inherits:
Base
  • Object
show all
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

Methods inherited from Base

instance

Instance Method Details

#count_digits(integer, base: 10) ⇒ Integer

Returns the number of digits in the given integer and base.

This method ignores minus sign for negative numbers. You can use the :base parameter to configure the numeric base for the string representation.

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.



87
88
89
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 87

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

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

Decomposes the given integer into its digits 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.



111
112
113
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 111

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

#integer?(obj) ⇒ Boolean

Returns true if the object is an Integer.

Examples:

IntegerTools.integer?(nil)
#=> false
IntegerTools.integer?([])
#=> false
IntegerTools.integer?({})
#=> false
IntegerTools.integer?(1)
#=> true

Parameters:

  • obj (Object)

    the object to test.

Returns:

  • (Boolean)

    true if the object is an Integer, otherwise false.



130
131
132
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 130

def integer?(obj)
  obj.is_a?(Integer)
end

#pluralize(count, single, plural = nil) ⇒ String

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

If the plural form is not given, passes the singular form to StringTools#pluralize.

Examples:

IntegerTools.pluralize 4, 'light'
#=> 'lights'

IntegerTools.pluralize 3, 'cow', 'kine'
#=> 'kine'

Parameters:

  • count (Integer)

    the number of items.

  • single (String)

    the singular form of the word or phrase.

  • plural (String) (defaults to: nil)

    the plural form of the word or phrase.

Returns:

  • (String)

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



152
153
154
155
156
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 152

def pluralize(count, single, plural = nil)
  plural ||= StringTools.pluralize(single)

  count == 1 ? 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.



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 177

def romanize(integer, additive: false)
  check_romanize_range(integer)

  digits(integer)
    .reverse
    .map
    .with_index do |digit, index|
      romanize_digit(additive:, digit: digit.to_i, tens: index)
    end
    .reverse
    .join
end