# Class: Integer

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

• Returns an array of integers containing the digits of the integer.

• Checks whether the integer goes evenly into all of the arguments.

• 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.

• 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]
``````

Returns:

• (Array)

the digits of the number as integers

 ``` 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.

#### Examples:

##### Check if 3 goes into some numbers
``````3.goes_into? 10  # => false
3.goes_into? 6   # => true``````
##### Check if 5 goes into every number in some sets of numbers
``````5.goes_into? 10, 12  # => false
5.goes_into? 10, 20  # => true``````

Parameters:

• nums (*Integer)

the integer(s) to check against

Returns:

• (Boolean)

whether or not 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.

#### Examples:

##### Check if 5 goes into every number in some sets of numbers
``````5.goes_into_any? 10, 20  # => true
5.goes_into_any? 10, 12  # => true
5.goes_into_any? 12, 21  # => false``````

Parameters:

• nums (*Integer)

the integer(s) to check against

Returns:

• (Boolean)

whether or not 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) ⇒ BooleanAlso known as: divisible_by?

Checks whether the integer is evenly divisible by all of the arguments.

#### Examples:

##### Check if some numbers are evenly divisible by 5
``````4.multiple_of? 5   # => false
30.multiple_of? 5  # => true``````
##### Check if some numbers are evenly divisible by both 4 and 7
``````10.multiple_of? 4, 7  # => false
56.multiple_of? 4, 7  # => true``````

Parameters:

• nums (*Integer)

the integer(s) to check against

Returns:

• (Boolean)

whether or not the integer is evenly divisible by all 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) ⇒ BooleanAlso known as: divisible_by_any?

Checks whether the integer is evenly divisible by any of the arguments.

#### Examples:

##### Project Euler #1 solution using Nutella
``(1...1000).sum { |n| n.multiple_of_any?(3, 5) }``

Parameters:

• nums (*Integer)

the integer(s) to check against

Returns:

• (Boolean)

whether or not the integer is evenly divisible by any 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"
``````

Returns:

• (String)

the ordinal form of the integer

 ``` 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```