Class: SleepingKingStudios::Tools::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
-
#count_digits(integer, base: 10) ⇒ Integer
Returns the number of digits in the given integer when represented in the specified base.
-
#digits(integer, base: 10) ⇒ Array<String>
Decomposes the given integer into its digits when represented in the given base.
-
#integer?(int) ⇒ Boolean
Returns true if the object is an Integer.
-
#pluralize(count, single, plural = nil) ⇒ String
Returns the singular or the plural value, depending on the provided item count.
-
#romanize(integer, additive: false) ⇒ String
Represents an integer between 1 and 4999 (inclusive) as a Roman numeral.
Methods inherited from Base
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.
85 86 87 |
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 85 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.
110 111 112 |
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 110 def digits(integer, base: 10) integer.to_s(base).chars end |
#integer?(int) ⇒ Boolean
Returns true if the object is an Integer.
119 120 121 |
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 119 def integer?(int) int.is_a?(Integer) end |
#pluralize(count, single, plural = nil) ⇒ String
Returns the singular or the plural value, depending on the provided item count.
136 137 138 139 140 |
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 136 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.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/sleeping_king_studios/tools/integer_tools.rb', line 161 def romanize(integer, additive: false) check_romanize_range(integer) digits(integer) .reverse .map .with_index do |digit, index| romanize_digit(additive: additive, digit: digit.to_i, tens: index) end .reverse .join end |