Sha256: 8202ea108d8c7219070e64f3b5d24377bdc364f3b0755c738901b2dce53da997

Contents?: true

Size: 930 Bytes

Versions: 30

Compression:

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

30 entries across 30 versions & 1 rubygems

Version Path
darkhelmet-darkext-0.10.0 lib/darkext/array.rb
darkhelmet-darkext-0.11.0 lib/darkext/array.rb
darkhelmet-darkext-0.11.1 lib/darkext/array.rb
darkhelmet-darkext-0.11.2 lib/darkext/array.rb
darkhelmet-darkext-0.5.1 lib/darkext/array.rb
darkhelmet-darkext-0.5.2 lib/darkext/array.rb
darkhelmet-darkext-0.5.3 lib/darkext/array.rb
darkhelmet-darkext-0.5.4 lib/darkext/array.rb
darkhelmet-darkext-0.5.5 lib/darkext/array.rb
darkhelmet-darkext-0.5.6 lib/darkext/array.rb
darkhelmet-darkext-0.6.0 lib/darkext/array.rb
darkhelmet-darkext-0.7.0 lib/darkext/array.rb
darkhelmet-darkext-0.7.1 lib/darkext/array.rb
darkhelmet-darkext-0.7.2 lib/darkext/array.rb
darkhelmet-darkext-0.7.3 lib/darkext/array.rb
darkhelmet-darkext-0.8.0 lib/darkext/array.rb
darkhelmet-darkext-0.8.1 lib/darkext/array.rb
darkhelmet-darkext-0.8.2 lib/darkext/array.rb
darkhelmet-darkext-0.8.3 lib/darkext/array.rb
darkhelmet-darkext-0.8.4 lib/darkext/array.rb