CREDIT Richard Laugesen
[] | -> | / |
Use division as a fetch notation. | ||
| | -> | merge |
unshift | -> | pot |
"Put On Top". This is an alias for unshift which puts an object#
on top of the stack. It is the converse of push.
a=[1,2,3] a.pot(9) #=> [9,1,2,3] a #=> [9,1,2,3] |
||
each_with_index | -> | each_with_key |
Increase polymorphysim between Hash and Array. | ||
shift | -> | pull |
Alias for shift which removes an object off first slot of an array. This is
the contrary of pop.
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.
a = ["a","y","z"] a.first! #=> "a" p a #=> ["y","z"] |
||
pop | -> | last! |
Alias for pop, which removes and returns the last element in an array.
a = [1,2] a.last! 3 p a #=> [1,2,3] |
||
index | -> | index_of |
Modifies #[] to also accept an array of indexes.
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.
a = ['a','b','c','d'] a[[1,-1]] = ['m','n'] #=> ['m','n'] a #=> ['a','m','c','n']
Same as at_rand, but acts in place removing a random element from the array.
a = [1,2,3,4] a.at_rand! #=> 2 a #=> [1,3,4]
Returns an array of the first element upto, but not including, the last element.
[1,2,3].body #=> [1,2]
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"]
The array is expected to be and array of arrays, which iterates through combinations of each in position.
a = [ [0,1], [2,3] ] a.each_combo { |c| p c }
produces
[0, 2] [0, 3] [1, 2] [1, 3]
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){ … }.
Returns the last index of the array. Returns nil is array has no elements.
[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.
[1,2,3,4,5].mid #=> 3 [1,2,3,4,5,6].mid #=> 4 [1,2,3,4,5,6].mid(-1) #=> 3 [1,2,3,4,5,6].mid(-2) #=> 2 [1,2,3,4,5,6].mid(1) #=> 5
In other words, If there are an even number of elements the higher-indexed of the two center elements is indexed as orgin (0).
Pad an array with a given value upto a given length.
[0,1,2].pad(6,"a") #=> [0,1,2,"a","a","a"]
If length is a negative number padding will be added to the beginning of the array.
[0,1,2].pad(-6,"a") #=> ["a","a","a",0,1,2]
Like pad but changes the array in place.
a = [0,1,2] a.pad!(6,"x") a #=> [0,1,2,"x","x","x"]
Similar to at_rand, but will return an array of randomly picked exclusive elements if given a number.
Returns the positive ordinal index given a cardinal position, 1 to n or -n to -1.
[1,2,3,4,5].pos(1) #=> 0 [1,2,3,4,5].pos(-1) #=> 4
Returns a random subset of an Array. If a number of elements is specified then returns that number of elements, otherwise returns a random number of elements upto the size of the Array.
By defualt the returned 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.
[1, 2, 3, 4].rand_subset(1) #=> [2] [1, 2, 3, 4].rand_subset(4) #=> [2, 1, 3, 4] [1, 2, 3, 4].rand_subset #=> [1, 3, 4] [1, 2, 3, 4].rand_subset #=> [2, 3]
Rotates an array‘s elements from back to front n times.
[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]
As with select but modifies the Array in place.
a = [1,2,3,4,5,6,7,8,9,10] a.select!{ |e| e % 2 == 0 } a #=> [2,4,6,8,10]
Converts a two-element associative array into a hash.
a = [ [:a,1], [:b,2] ] a.to_h #=> { :a=>1, :b=>2 }
If arrayed is set it will maintain trailing arrays.
a = [ [:a,1,2], [:b,3] ] a.to_h(true) #=> { :a=>[1,2], :b=>[3] }
NOTE: the use of a values parameter has been deprecated because that functionality is as simple as:
array1.zip(array2).to_h
Converts an array into a hash.
This methd is just like Enumerable#to_h. But has been moved in order to accomodate associtiave arrays. We use to_hash becasue an Array is, in essence, a hash, just with a restricted set of keys and maintained order.
a = [ :a, :b, :c ] a.to_hash #=> { 0=>:a, 1=>:b, 2=>:c }
Returns a JSON string containing a JSON array, that is unparsed from this Array instance. state is a JSON::State object, that can also be used to configure the produced JSON string output further. depth is used to find out nesting depth, to indent accordingly.