include? | -> | contains? |
Alias for include?. | ||
shift | -> | first! |
Alias for shift, which removes and returns the first element in an array.
a = ["a","y","z"] a.first! #=> "a" a #=> ["y","z"] CREDIT: Trans |
||
pop | -> | last! |
Alias for pop, which removes and returns the last element in an array.
a = [1,2,3] a.last! #=> 3 a #=> [1,2] CREDIT: Trans |
||
| | -> | merge |
Alias for |.
[1,2].merge([2,3]) #=> [1,2,3] |
||
shift | -> | pull |
Alias for shift which removes an object off first slot of an array. This is the opposite of pop. | ||
[]= | -> | store |
Store a value at a given index. Store is
an alias for #[]=.
a = [] a.store(1, "A") a[1] #=> "A" |
||
empty? | -> | blank? |
Returns the value previous to the given value. The value previous to the first is the last. Returns nil if the given value is not in the array.
Examples
sequence = ['a', 'b', 'c'] sequence.before('a') #=> 'c' sequence.before('b') #=> 'a' sequence.before('c') #=> 'b' sequence.before('d') #=> nil
CREDIT: Tyler Rick
Yields the block to each unique combination of n elements.
a = %w|a b c d| a.combination(3)
produces
[["a", "b", "c"], ["a", "b", "d"], ["a", "c", "d"], ["b", "c", "d"]]
CREDIT: Florian Gross
Returns all items that are equal in terms of the supplied block. If no block is given objects are considered to be equal if they return the same value for Object#hash and if obj1 == obj2.
[1, 2, 2, 3, 4, 4].commonality #=> { 2 => [2, 2], 4 => [4, 4] } ["foo", "bar", "a"].commonality { |str| str.length } #=> { 3 => ["foo", "bar"] }
This can be useful, for instance, in determining all persons that share their last name with another person …
persons.collisions { |person| person.last_name }
CREDIT: Florian Gross
This is more advanced form of join. It allows for fine control of separators.
NOTE: The old version used to default its separator to ", " and default the terminating separator to " and ". This is no longer the case. You must specifically provide these parameters.
If no paramters are given, it acts like join but will a space separator.
[1,2,3].conjoin #=> "1 2 3"
Use comma+space and ‘and’ on tail.
[1,2,3].conjoin(', ', ' and ') #=> "1, 2 and 3"
Use comma+space and ‘or’ on tail using :last option.
[1,2,3].conjoin(', ', :last => ' or ') #=> "1, 2 or 3"
Use semicolon+space and ampersand on tail using index.
[1,2,3].conjoin('; ', -1 => ' & ') #=> "1; 2 & 3"
Can take a block to determine separator.
[1,2,3,4].conjoin{ |i, a, b| i % 2 == 0 ? '.' : '-' } #=> "1.2-3.4"
This makes very esoteric transformation possible.
[1,1,2,2].conjoin{ |i, a, b| a == b ? '=' : ' != ' } #=> "1=1 != 2=2" [1,2,3,4].conjoin{ |i, x, y| "<#{i} #{x} #{y}>" } #=> "1<0 1 2>2<1 2 3>3<2 3 4>4"
There are also spacing options. Providing the :space option pads the separators.
[1,2,3].conjoin(',', '&', :space=>2) #=> "1 , 2 & 3"
And the :spacer option can set an alternate spacing string.
[1,2,3].conjoin('|', '>', :space=>2, :spacer=>'-') #=> "1--|--2-->--3"
CREDIT: Trans
Delete multiple values from array.
a = [1,2,3,4] a.delete_values(1,2) #=> [1,2] a #=> [3,4]
CREDIT: Trans
Divide on matching pattern.
['a1','b1','a2','b2'].divide(/^a/) #=> [['a1','b1'],['a2','b2']]
CREDIT: Trans
Extracts options from a set of arguments. Removes and returns the last element in the array if it‘s a hash, otherwise returns a blank hash.
def options(*args) args.extract_options! end options(1, 2) # => {} options(1, 2, :a => :b) # => {:a=>:b}
Returns the maximum possible Shannon entropy of the array with given size assuming that it is an "order-0" source (each element is selected independently of the next).
CREDIT: Derek
Allows index to accept a block.
['a', 'b', 'c'].index{ |x| x.upcase == 'C' } #=> 2
IMPORTANT: This is one of the few core overrides in Facets.
Returns the only element in the array. Raises an IndexError if the array‘s size is not 1.
[5].only # => 5 expect IndexError do [1,2,3].only end expect IndexError do [].only end
CREDIT: Gavin Sinclair, Noah Gibbs
Pad an array with a given value up to 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]
CREDIT: Richard Laugesen
Like pad but changes the array in place.
a = [0,1,2] a.pad!(6,"x") a #=> [0,1,2,"x","x","x"]
CREDIT: Richard Laugesen
Returns the percentile value for percentile p; nil if array is empty.
p should be expressed as an integer; percentile(90) returns the 90th percentile of the array.
CREDT: ?
Permutation provids the possible orders of an enumerable. Each is indexed by a permutation number. The maximum number of arrangements is the factorial of the size of the array.
[1,2].permutation(2).to_a #=> [[1,2], [2,1]]
CREDIT: Shin-ichiro Hara
Generates a hash mapping each unique element in the array to the relative frequency, i.e. the probablity, of it appearence.
[:a, :b, :c, :c].probability #=> {:a=> 0.25, :b=>0.25, :c=>0.5}
CREDIT: Brian Schröder
Provides the cartesian product of two or more arrays.
a = [1,2].product([4,5]) a #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
CREDIT: Thomas Hafner
Apply a block to array, and recursively apply that block to each sub-array or types.
arr = ["a", ["b", "c", nil], nil] arr.recurse{ |a| a.compact! } #=> ["a", ["b", "c"]]
Apply a method to array, and recursively apply that method to each sub-array or types.
arr = ["a", ["b", "c"]] arr.recursively.map{ |v| v.to_sym } #=> [:a, [:b, :c]]
By default the sub-types are passed thru uneffected. Passing a block to recursively changes this.
arr = ["a", ["b", "c"]] arr.recursively{ |a| a.reverse }.map{ |v| v.to_sym } #=> [:a, [:c, :b]]
TODO: Return Enumerator if no yld block is given ?
Same as rotate, but acts in place.
a = [1,2,3] a.rotate! a #=> [2,3,1]
CREDIT: Florian Gross, Thomas Sawyer
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]
CREDIT: Gavin Sinclair
Convert an array into command line parameters. The array is accepted in the format of Ruby method arguments —ie. [arg1, arg2, …, hash]
Splice acts a combination of slice! and store. If two arguments are given it calls store. If a single argument is given it calls slice!.
a = [1,2,3] a.splice(1) #=> 2 a #=> [1,3] a = [1,2,3] a.splice(1,4) #=> 4 a #=>[1,4,3]
CREDIT: Trans
Split on matching pattern. Unlike divide this does not include matching elements.
['a1','a2','b1','a3','b2','a4'].split(/^b/) #=> [['a1','a2'],['a3'],['a4']]
CREDIT: Trans
Converts an array into a hash. Converting an array into a hash is not a one-to-one conversion, for this reason to_h examines at the array being converted and then dispatches the conversion to the most sutiable specialized function. There are three possiblities for this.
If the array is a collection of perfect pairs, like that which Hash#to_a generates, then conversion is handled by to_h_flat.
a = [ [:a,1], [:b,2] ] a.to_h #=> { :a=>1, :b=>2 }
If the array contains only arrays, but are not perfect pairs, then to_h_multi is called.
a = [ [:a,1,2], [:b,2], [:c], [:d] ] a.to_h #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
If the array contians objects other then arrays then the to_h_splat method is called.
a = [ [:a,1,2], 2, :b, [:c,3], 9 ] a.to_h #=> { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
Finally, a particular dispatch can be forced by specifying the mode of conversion, eg. +:multi+, +:splat+, +:flat+, +:assoc+, etc.
Setting mode to true is the same as setting it +:multi+. This has been left in for backward compatability.
NOTE: The use of a values parameter has been deprecated because that functionality is as simple as …
array1.zip(array2).to_h
CREDIT: Robert Klemme, Trans
When a mixed or multi-element accociative array is used, the result is as follows:
a = [ [:a,1,2], [:b,2], [:c], :d ] a.to_h #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
If the first entry of any subelements are the same, then the value will be set to the last occuring value.
a = [ :x, [:x], [:x,1,2], [:x,3], [:x,4] ] a.to_h_assoc #=> { :x=>[4] }
Converts an array into a hash. Converting an array into a hash is not a one-to-one conversion, for this reason to_h examines at the array being converted and then dispatches the conversion to the most sutiable specialized function. There are three possiblities for this.
If the array is a collection of perfect pairs, like that which Hash#to_a generates, then conversion is handled by to_h_flat.
a = [ [:a,1], [:b,2] ] a.to_h_auto #=> { :a=>1, :b=>2 }
If the array contains only arrays, but are not perfect pairs, then to_h_multi is called.
a = [ [:a,1,2], [:b,2], [:c], [:d] ] a.to_h_auto #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
If the array contians objects other then arrays then the to_h_splat method is called.
a = [ [:a,1,2], 2, :b, [:c,3], 9 ] a.to_h_auto #=> { [:a,1,2]=>2, :b=>[:c,3], 9=>nil }
When a mixed or multi-element accociative array is used, the result is as follows:
a = [ [:a,1,2], [:b,2], [:c], :d ] a.to_h #=> { :a=>[1,2], :b=>[2], :c=>[], :d=>[] }
If the first entry of the subelements is the same, then the values will be merged using concat.
a = [ [:a,1,2], [:a,3], [:a,4], [:a], :a ] a.to_h_multi #=> { :a=>[1,2,3,4] }
This is equivalent to Hash[*array], but it will pad the array with a nil object if there are not an even number of elements.
a = [:a,1,:b,2,:c] a.to_h_splat #=> { :a=>1, :b=>2, :c=>nil }
Returns a new array created by traversing the array and its sub-arrays, executing the given block on the elements.
h = ["A", "B", ["X", "Y"]] g = h.traverse{ |e| e.downcase } g #=> ["a", "b", ["x", "y"]]
This is the same as recursive.map and will likely be deprecated in the future because of it.
CREDIT: Trans
Like recursive_map, but will change the array in place.
h = ["A", "B", ["X", "Y"]] h.traverse!{ |e| e.downcase } h #=> ["a", "b", ["x", "y"]]
CREDIT: Trans