Class: Integer
- Defined in:
- lib/nutella/core_ext/integer/digits.rb,
lib/nutella/core_ext/integer/format.rb,
lib/nutella/core_ext/integer/multiples.rb
Instance Method Summary collapse
-
#digits ⇒ Array
Returns an array of integers containing the digits of the integer.
-
#goes_into?(*nums) ⇒ Boolean
Checks whether the integer goes evenly into all of the arguments.
-
#goes_into_any?(*nums) ⇒ Boolean
Checks whether the integer goes evenly into any of the arguments.
-
#multiple_of?(*nums) ⇒ Boolean
(also: #divisible_by?)
Checks whether the integer is evenly divisible by all of the arguments.
-
#multiple_of_any?(*nums) ⇒ Boolean
(also: #divisible_by_any?)
Checks whether the integer is evenly divisible by any of the arguments.
-
#ordinalize ⇒ String
Returns a string with the ordinal form of the integer.
Instance Method Details
#digits ⇒ Array
Returns an array of integers containing the digits of the integer. The "-"
will be trimmed if the number is negative.
120.digits # => [1, 2, 0]
-35.digits # => [3, 5]
9 10 11 |
# File 'lib/nutella/core_ext/integer/digits.rb', line 9 def digits abs.to_s.chars.map &:to_i end |
#goes_into?(*nums) ⇒ Boolean
Checks whether the integer goes evenly into all of the arguments.
15 16 17 |
# File 'lib/nutella/core_ext/integer/multiples.rb', line 15 def goes_into?(*nums) nums.all? { |n| (!zero? && n % self == 0) || n.zero? } end |
#goes_into_any?(*nums) ⇒ Boolean
Checks whether the integer goes evenly into any of the arguments.
29 30 31 |
# File 'lib/nutella/core_ext/integer/multiples.rb', line 29 def goes_into_any?(*nums) nums.any? { |n| goes_into? n } end |
#multiple_of?(*nums) ⇒ Boolean Also known as: divisible_by?
Checks whether the integer is evenly divisible by all of the arguments.
46 47 48 |
# File 'lib/nutella/core_ext/integer/multiples.rb', line 46 def multiple_of?(*nums) nums.all? { |n| (!n.zero? && self % n == 0) || zero? } end |
#multiple_of_any?(*nums) ⇒ Boolean Also known as: divisible_by_any?
Checks whether the integer is evenly divisible by any of the arguments.
59 60 61 |
# File 'lib/nutella/core_ext/integer/multiples.rb', line 59 def multiple_of_any?(*nums) nums.any? { |n| multiple_of? n } end |
#ordinalize ⇒ String
Returns a string with the ordinal form of the integer.
1.ordinalize # => "1st"
2.ordinalize # => "2nd"
9.ordinalize # => "9th"
43.ordinalize # => "43rd"
10 11 12 13 14 15 16 |
# File 'lib/nutella/core_ext/integer/format.rb', line 10 def ordinalize self.to_s + if abs % 100 / 10 != 1 && (1..3).include?(last = abs % 10) {1 => "st", 2 => "nd", 3 => "rd"}[last] else "th" end end |