Class Array
In: lib/facet/array/pull.rb
lib/facet/array/each_unique_pair.rb
lib/facet/array/%5B%5D%3D.rb
lib/facet/array/first%21.rb
lib/facet/array/first%3D.rb
lib/facet/array/select%21.rb
lib/facet/array/tail.rb
lib/facet/array/%5B%5D.rb
lib/facet/array/delete_values.rb
lib/facet/array/rotate%21.rb
lib/facet/array/shuffle%21.rb
lib/facet/array/last_index.rb
lib/facet/array/thru.rb
lib/facet/array/at_rand.rb
lib/facet/array/foot.rb
lib/facet/array/range.rb
lib/facet/array/to_b.rb
lib/facet/array/to_h.rb
lib/facet/array/delete_values_at.rb
lib/facet/array/head.rb
lib/facet/array/pos.rb
lib/facet/array/rotate.rb
lib/facet/array/top.rb
lib/facet/array/permute.rb
lib/facet/array/shuffle.rb
lib/facet/array/each_combination.rb
lib/facet/array/combinations.rb
lib/facet/array/middle.rb
lib/facet/array/last%21.rb
lib/facet/array/last%3D.rb
lib/facet/array/body.rb
lib/facet/array/store.rb
lib/facet/enumerable/each_slice.rb
Parent: Object

Methods

[]   []=   at_rand   at_rand!   body   combinations   delete_values   delete_values_at   each_by   each_combination   each_slice   each_unique_pair   first=   foot   head   index_range   last=   last_index   middle   permute   pick   pos   rand_index   range   rotate   rotate!   select!   shuffle   shuffle!   store   tail   thru   to_b   to_h  

External Aliases

shift -> pull
  Alias for shift, which removes and returns the top (i.e. first) element of an array.
  require 'facet/array/pull'

  a=[1,2,3]
  a.pull      #=> 1
  a           #=> [2,3]
shift -> first!
  Alias for shift, which removes and returns the first element in an array.
  require 'facet/array/first'

  a = ["a","y","z"]
  a.first!      #=> "a"
  p a           #=> ["y","z"]
unshift -> top
  Alias for unshift which puts an object on the top (first) of an array. This is the oppposite of push.
  require 'facet/array/top'

  a=[1,2,3]
  a.top(9)   #=> [9,1,2,3]
  a          #=> [9,1,2,3]
pop -> last!
  Alias for pop, which removes and returns the last element in an array.
  require 'facet/array/last'

  a = [1,2]
  a.last! 3
  p a           #=> [1,2,3]

Public Class methods

Produces an array of arrays of all possible combinations of the given arrays in the position given. (Explain me better?)

  require 'facet/arrat/combinations'

  a = %w|a b|
  b = %w|a x|
  c = %w|x y|
  Array.combinations(a, b, c).each { |x| p x }

produces

  ["a", "a", "x"]
  ["a", "a", "y"]
  ["a", "x", "x"]
  ["a", "x", "y"]
  ["b", "a", "x"]
  ["b", "a", "y"]
  ["b", "x", "x"]
  ["b", "x", "y"]

Public Instance methods

Modifies #[] to accept an array of indexes.

  require 'facet/array/index-op'

  a = ['a','b','c','d','e','f']

  a[[1]]      #=> ['b']
  a[[1,1]]    #=> ['b','b']
  a[[1,-1]]   #=> ['b','f']
  a[[0,2,4]]  #=> ['a','c','e']

Modifies #[]= to accept an array of indexes for assignment.

  require 'facet/array/index-op'

  a = ['a','b','c','d']

  a[[1]] = 'x'             #=> ['x']
  a                        #=> ['a','x','c','d']

  a[[1,-1]] = ['m','n']    #=> ['m','n']
  a                        #=> ['a','m','c','n']

Return random number of value(s) from an Array. If the number of elements is not specified (or is nil) then returns a single element, otherwise returns a random array of elements. By defualt the values are exclusive of each other, but if exclusive is set to false, the same values can be choosen more than once.

When exclusive is true (the default) and the number given is greater than the size of the array, then all values are returned.

  require 'facet/array/at_rand'

  [1, 2, 3, 4].at_rand           #=> 2
  [1, 2, 3, 4].at_rand(1)        #=> [2]
  [1, 2, 3, 4].at_rand(2)        #=> [1,4]
  [1, 2, 3, 4].at_rand(3)        #=> [4,3,2]
  [1, 2, 3, 4].at_rand(3,false)  #=> [4,4,2]

Same as at_rand, but acts in place removing the element from the array. This is necessarily exclusive.

  require 'facet/array/at_rand'

  a = [1,2,3,4]
  a.at_rand!       #=> 2
  a                #=> [1,3,4]

  a = [1,2,3,4]
  a.at_rand!(2)    #=> [2,1]
  a                #=> [3,4]

Returns an array of the first element upto, but not including, the last element.

  require 'facet/array/body'

  [1,2,3].body  #=> [1,2]

Delete multiple values from array.

  require 'facet/array/delete_values'

  a = [1,2,3,4]
  a.delete_values(1,2)   #=> [1,2]
  a                      #=> [3,4]

Delete multiple values from array given indexes or index range.

  require 'facet/array/delete_at'

  a = [1,2,3,4]
  a.delete_values_at(1,2)   #=> [2,3]
  a                         #=> [1,4]
  a = [1,2,3,4]
  a.delete_values_at(0..2)  #=> [1,2,3]
  a                         #=> [4]
each_by(n=nil, &yld)

Alias for each_slice

Yields the block to each unique combination of n elements.

  a = %w|a b c d|
  a.each_combination(3) do |c|
    p c
  end

produces

  ["a", "b", "c"]
  ["a", "b", "d"]
  ["a", "c", "d"]
  ["b", "c", "d"]

Iterates over n elements at a time. If n is not given then the arity of the block determines the slicing quantity.

  require 'facet/array/each_slice'

  [1, 2, 3, 4].each_slice(2){ |a,b| ... }

This is a specialized version Enumerable#each_slice but optimized specifically for Array.

Processes each unique pair (of indices, not value) in the array by yielding them to the supplied block.

  a = [1,2,3,4]
  a.each_unique_pair{ |a,b| puts a+','+b }

produces

  1,2
  1,3
  1,4
  2,3
  2,4
  3,4

This does not guarantee the uniqueness of values. For example:

  a = [1,2,1]
  a.each_unique_pair{ |a,b| puts a+','+b }

prduces

  1,2
  1,1
  2,1

This is equivalent to each_combination(2){ … }.

Change the first element.

  require 'facet/array/first'

  a = ["a","y","z"]
  a.first = "x"
  p a           #=> ["x","y","z"]

Like last but returns the last element in an array.

  [1,2,3].foot  #=> [3]

Like first but returns the first element in a new array.

  [1,2,3].head  #=> [1]
index_range(a=nil,z=nil)

Alias for range

Change the last element.

  require 'facet/array/last'

  a = [1,2,5]
  a.last = 3
  p a           #=> [1,2,3]

Returns the last index of the array. Returns nil is array has no elements.

  require 'facet/array/last_index'

  [1,2,3,4,5].last_index  #=> 4

Returns the middle element of an array, or the element offset from middle if the parameter is given. Even-sized arrays, not having an exact middle, return the middle-right element.

  require 'facet/array/middle'

  [1,2,3,4,5].middle        #=> 3
  [1,2,3,4,5,6].middle      #=> 4
  [1,2,3,4,5,6].middle(-1)  #=> 3

In other words, If there are an even number of elements the higher-indexed of the two center elements is indexed as orgin (0).

Applys a block to each possible permutation of an array.

  %w[a b c].permute { |x| puts(x.join('')) }

produces

  abc
  acb
  bac
  bca
  cab
  cba
pick(number=nil)

Alias for at_rand!

Returns the positive ordinal index given a cardinal position, 1 to n or -n to -1.

  require 'facet/array/pos'

  [1,2,3,4,5].pos(1)   #=> 0
  [1,2,3,4,5].pos(-1)  #=> 4

Return random index(es) from an Array. If the number of elements is not specified (or is nil) then returns just a single index.

When exclusive is true (the default) and the number given is greater than the size of the array, then all indexes are returned.

  require 'facet/array/at_rand'

  ["a", "b", "c", "d"].rand_index           #=> 1
  ["a", "b", "c", "d"].rand_index           #=> 2
  ["a", "b", "c", "d"].rand_index(2)        #=> [2,1]
  ["a", "b", "c", "d"].rand_index(3)        #=> [1,3,2]
  ["a", "b", "c", "d"].rand_index(3,false)  #=> [4,3,4]

Returns the index range between two elements. If no elements are given, returns the range from first to last.

  require 'facet/array/range'

  ['a','b','c','d'].range            #=> 0..3
  ['a','b','c','d'].range('b','d')   #=> 1..2

Rotates an array’s elements from back to front n times.

  require 'facet/array/rotate'

  [1,2,3].rotate      #=> [3,1,2]
  [3,1,2].rotate      #=> [2,3,1]
  [3,1,2].rotate      #=> [1,2,3]
  [1,2,3].rotate(3)   #=> [1,2,3]

A negative parameter reverses the order from front to back.

  [1,2,3].rotate(-1)  #=> [2,3,1]

Same as rotate, but acts in place.

  require 'facet/array/rotate'

  a = [1,2,3]
  a.rotate
  a  #=> [3,1,2]

As with select but modifies the Array in place.

  require 'facet/array/select!'

  a = [1,2,3,4,5,6,7,8,9,10]
  a.select!{ |e| e % 2 == 0 }
  a  #=> [2,4,6,8,10]

Randomize the order of an array.

  require 'facet/array/shuffle'

  [1,2,3,4].shuffle  #=> [2,4,1,3]

As with shuffle but modifies the array in place. The algorithm used here is known as a Fisher-Yates shuffle.

  require 'facet/array/shuffle'

  a = [1,2,3,4]
  a.shuffle!
  a  #=> [2,4,1,3]
store(*args)

Alias for #[]=

Returns an array from second element to last element.

  [1,2,3].foot  #=> [2,3]

Fetch values from a start index thru an end index.

  require 'facet/array/upto'

  [1,2,3,4,5].thru(0,2)  #=> [1,2,3]
  [1,2,3,4,5].thru(2,4)  #=> [3,4,5]

Boolean conversion for not empty?

Produces a hash for an Array, or two arrays.

If no parameter is given, to_h expects the receiving array to be associative.

  require 'facet/array/to_h'

  a1 = [ [:a,1], [:b,2] ]
  a1.to_h  #=> { :a=>1, :b=>2 }

If an array is given it is considered values and zipped with the receiver, to produce the hash.

  a1 = [ :a, :b ]
  a2 = [ 1, 2 ]
  a1.to_h(a2)  #=> { :a=>1, :b=>2 }

[Validate]