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