class Array

Public Instance Methods

average()
Alias for: mean
duplicates() click to toggle source

Returns an array of the duplicate elements.

Examples

[1, 2, 3, 2, 4, 1, 5].duplicates #=> [1, 2]
[1, 2, 3, 4, 2, 4].duplicates    #=> [2, 4]
# File core_extensions/array/duplicates.rb, line 10
def duplicates
  self.select { |element| self.count(element) > 1 }.uniq
end
include_all?(*elements) click to toggle source

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
include_any?(*elements) click to toggle source

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
include_none?(*elements) click to toggle source

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
includes_index?(index) click to toggle source

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
mean() click to toggle source

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
Also aliased as: average
round(precision = 2) click to toggle source

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 of precision.

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
stddev() click to toggle source

Returns the standard deviation of the array of Numeric.

Examples

[1, 2, 3, 4, 5].stddev #=> 1.4142135623730951
[1.0, 2.0, 3.0].stddev #=> 0.816496580927726
# File core_extensions/array/math.rb, line 50
def stddev
  Math.sqrt(variance)
end
take!(n) click to toggle source

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
variance() click to toggle source

Returns the variance of the array of Numeric.

Examples

[1, 2, 3, 4, 5].variance #=> 2.0
[1.0, 2.0, 3.0].variance #=> 0.6666666666666666
# File core_extensions/array/math.rb, line 38
def variance
  return 0 if empty?

  map { |element| (element - mean) ** 2 }.sum / length
end