class Array
Public Instance Methods
Returns whether the array contains all of the elements
.
Arguments¶ ↑
-
elements
-elements
that needs to be checked in the array.
Examples¶ ↑
[1, 2, 3].include_all?(1, 2) #=> true [1, 2, 3].include_all?(1, 4) #=> false [1, 2, 3].include_all?(4, 5) #=> false
# File core_extensions/array/inclusion.rb, line 45 def include_all?(*elements) (elements - self).empty? end
Returns whether the array contains any of the elements
.
Arguments¶ ↑
-
elements
-elements
that needs to be checked in the array.
Examples¶ ↑
[1, 2, 3].include_any?(1, 2) #=> true [1, 2, 3].include_any?(1, 4) #=> true [1, 2, 3].include_any?(4, 5) #=> false
# File core_extensions/array/inclusion.rb, line 15 def include_any?(*elements) !(self & elements).empty? end
Returns whether the array contains none of the elements
.
Arguments¶ ↑
-
elements
-elements
that needs to be checked in the array.
Examples¶ ↑
[1, 2, 3].include_none?(1, 2) #=> false [1, 2, 3].include_none?(1, 4) #=> false [1, 2, 3].include_none?(4, 5) #=> true
# File core_extensions/array/inclusion.rb, line 30 def include_none?(*elements) (self & elements).empty? end
Returns whether the array has a value at the specified index
.
Arguments¶ ↑
-
index
-index
that needs to be checked in the array.
Examples¶ ↑
[1, 2, 3].includes_index?(-4) #=> false [1, 2, 3].includes_index?(-3) #=> true [1, 2, 3].includes_index?(1) #=> true [1, 2, 3].includes_index?(2) #=> true [1, 2, 3].includes_index?(3) #=> false
# File core_extensions/array/inclusion.rb, line 62 def includes_index?(index) (-self.length...self.length).cover?(index) end
Returns the mean of the array of Numeric
.
Examples¶ ↑
[1, 2, 3, 4, 5].mean #=> 3.0 [1.0, 2.0, 3.0].mean #=> 2.0
# File core_extensions/array/math.rb, line 27 def mean sum.to_f / size end
Rounds each element of the array up to specified precision
.
If precision
is zero
, array elements will be rounded to integers.
Attributes¶ ↑
-
precision
- Returns float rounded to the nearest value with a precision ofprecision
.
Examples¶ ↑
[1.342, 2.876, 3.546, 5.623, 5.245].round #=> [1.34, 2.88, 3.55, 5.62, 5.25] [1.342, 2.876, 3.546, 5.623, 5.245].round(1) #=> [1.3, 2.9, 3.5, 5.6, 5.2] [1.342, 2.876, 3.546, 5.623, 5.245].round(0) #=> [1, 3, 4, 6, 5]
# File core_extensions/array/math.rb, line 17 def round(precision = 2) map { |element| element.round(precision) } end
Alters the array by removing first n
elements.
If a negative number is given, raises an ArgumentError
.
Attributes¶ ↑
-
n
- Number of elements to take from the array.
Examples¶ ↑
[].take!(3) # => [] [1, 2, 3, 4, 5].take!(3) #=> [1, 2, 3] [1, 2, 3, 4, 5].take!(6) #=> [1, 2, 3, 4, 5]
# File core_extensions/array/delete.rb, line 17 def take!(n) replace(take(n)) end