Class Array
In: lib/facet/array/pull.rb
lib/facet/array/[].rb
lib/facet/array/first&last.rb
lib/facet/array/head&tail.rb
lib/facet/array/select!.rb
lib/facet/array/delete_values.rb
lib/facet/array/last_index.rb
lib/facet/array/thru.rb
lib/facet/array/at_rand.rb
lib/facet/array/range.rb
lib/facet/array/get.rb
lib/facet/array/to_b.rb
lib/facet/array/to_h.rb
lib/facet/array/[[]].rb
lib/facet/array/pos.rb
lib/facet/array/rotate.rb
lib/facet/array/shuffle.rb
lib/facet/array/combinations.rb
lib/facet/array/middle.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   get   head   index_range   last=   last_index   middle   pick   pos   rand_index   range   rotate   rotate!   select!   shuffle   shuffle!   store   tail   thru   to_b   to_h  

External Aliases

shift -> pull
  Alias for shift.
  require 'facet/array/pull'

  a=[1,2,3]
  a.pull      #=> 1
  a           #=> [2,3]
unshift -> put
  Alias for unshift.
  require 'facet/array/pull'

  a=[1,2,3]
  a.put(9)   #=> [9,1,2,3]
  a          #=> [9,1,2,3]
unshift -> first!
  Alias for unshift, which pushes an element onto the front of an array.
  require 'facet/array/first&last='

  a = ["a","y","z"]
  a.first! "x"
  p a           #=> ["x","a","y","z"]
push -> last!
  Alias for push.
  require 'facet/array/first&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 a multiple indexes.

  require 'facet/array/[]'

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

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

WARNING: Use with caution! This will cause other routines, which use the array[start,length] notation to no longer work. If you feel passinate about it, make sure all other routines use slice or facet’s own get method instead.

Modifies #[] to accept an Array of indexes.

  require 'facet/array/[[]]'

  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']

Add slicing to element assignment operator. If an Array is passed as the assignment then each element will be assigned to the corresponding index. The shorter of the two determine how many to assign.

  require "facet/array/[]"

  h = [1,2,3]

  h[0,1] = [10,11]       #=> [10,11]
  h                      #=> [10,11,3]

  h[0,1] = [12,13,14]    #=> [12,13]
  h                      #=> [12,13,3]

  h[0,1] = [15]          #=> [15]
  h                      #=> [15,13,3]

If any other object is passed as assignment then all indexes will be assigned that same object.

  h[0,1] = 99            #=> [99,99]
  h                      #=> [99,99,3]

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

  require 'facet/array/[[]]'

  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 array of first element upto, but not including, the last element.

  [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_values'

  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&last='

  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]

Fetch values given a start index and a length.

  require 'facet/array/get'

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

Like first but returns the first element in an array.

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

Alias for range

Change the last element.

  require 'facet/array/first&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).

pick(number=nil)

Alias for at_rand!

Returns positive index of an element based on cardinal positions 1 to n and 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]