| 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] |
||
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"]
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]
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"]
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
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]
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]
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]
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 }