Sha256: 9ba100c5c8bdcc59159c442565afd759336af83af04269f177d1af13da98f8b1

Contents?: true

Size: 1021 Bytes

Versions: 7

Compression:

Stored size: 1021 Bytes

Contents

require 'darkext/numeric'
require 'darkext/symbol'

class Array
  # Rotates the array left by n elements
  def rotate(n = 1)
    return if self.size.zero?
    n.times { self.push(self.shift) }
  end

  # Rotates the array right by n elements
  def rotate_reverse(n = 1)
    return if self.size.zero?
    n.times { self.unshift(self.pop) }
  end

  # Sums the array
  def sum
    self.inject(:+)
  end

  # Finds the product of the array
  def product
    self.inject(:*)
  end

  # Collects the squares of each value in the array
  def squares
    self.map(&:square)
  end

  # Destructively collects the squares
  def squares!
    self.map!(&:square)
  end

  # Finds the sum of squares of the array
  def sum_of_squares
    self.squares.sum
  end

  # Picks a random value from the array
  def random
    self[rand(self.size)]
  end
  alias :pick :random

  # Randomizes the array
  def randomize
    self.sort_by { rand }
  end

  # Destructively randomizes
  def randomize!
    self.replace(self.randomize)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
darkhelmet-darkext-0.3.1 lib/darkext/array.rb
darkhelmet-darkext-0.3.2 lib/darkext/array.rb
darkhelmet-darkext-0.4.0 lib/darkext/array.rb
darkhelmet-darkext-0.4.1 lib/darkext/array.rb
darkhelmet-darkext-0.4.2 lib/darkext/array.rb
darkhelmet-darkext-0.4.3 lib/darkext/array.rb
darkhelmet-darkext-0.5.0 lib/darkext/array.rb