Sha256: 02dae2da00e9c957831de65c1be6897d3c6e0cde058990b94269f740f2a86ea5

Contents?: true

Size: 932 Bytes

Versions: 2

Compression:

Stored size: 932 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

  # 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

2 entries across 2 versions & 2 rubygems

Version Path
darkhelmet-darkext-0.12.0 lib/darkext/array.rb
darkext-0.12.0 lib/darkext/array.rb