Tools

A library of utility services and concerns.

Class: SleepingKingStudios::Tools::IntegerTools

Parent Namespace
SleepingKingStudios::Tools
Inherited Classes
SleepingKingStudios::Tools::Base > Object
Defined In
lib/sleeping_king_studios/tools/integer_tools.rb

Table Of Contents

Overview

Tools for working with integers.

Back To Top

Constants

ROMANIZE_MAX

= 4999

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

ROMANIZE_MIN

= 1

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

Back To Top

Class Methods

.instance => SleepingKingStudios::Tools::Base

Inherited From
SleepingKingStudios::Tools::Base

Returns

Back To Top

Instance Methods

#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

Returns

#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

Returns

#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

Returns

#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

Returns

#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

Returns

Raises

Back To Top


Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools