Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ruby:array [2014/04/19 15:41] (current)
Line 1: Line 1:
 +====== Ruby arrays ======
 +
 +Here I try to make sense of the plethora of methods [[http://​www.ruby-doc.org/​core-2.0/​Array.html|Ruby arrays]] have.
 +
 +===== Element access =====
 +
 +  * #[]
 +  * #[]= (assignment)
 +  * #at (takes negative indices, not as versatile as #[])
 +  * #fetch (throws IndexError or returns default value)
 +  * #first
 +  * #last
 +  * #sample
 +  * #slice (see also #slice!)
 +  * #values_at (takes multiple indices or ranges)
 +
 +These are best understood together. They are not destructive!
 +
 +  * #drop
 +  * #drop_while
 +  * #take
 +  * #take_while
 +
 +===== Information =====
 +
 +  * #count
 +  * #empty?
 +  * #frozen?
 +  * #hash
 +  * #inspect
 +  * #length
 +  * #size
 +
 +===== Iteration =====
 +
 +  * #cycle (iterate many times or forever)
 +  * #each
 +  * #each_index (like #each, but passes an index)
 +  * #​reverse_each
 +
 +===== Search =====
 +
 +  * #assoc
 +  * #bsearch
 +  * #find_index
 +  * #include?
 +  * #index (alias of #​find_index)
 +  * #rassoc
 +  * #rindex
 +
 +===== Insertion =====
 +
 +  * #<< (append)
 +  * #concat (appends all elements in given array)
 +  * #insert (at a given index)
 +  * #push (append)
 +  * #unshift (add at beginning)
 +
 +===== Deletion =====
 +
 +Just delete it all:
 +
 +  * #clear
 +  * #replace (completely replaces the contents of self by the given array)
 +
 +Remove or keep depending on a condition:
 +
 +  * #delete (a given object)
 +  * #delete_if (satisfies block, see also #reject)
 +  * #keep_if (satisfies block, see also #select)
 +  * #reject (see also #delete_if)
 +  * #reject!
 +  * #select (see also #keep_if)
 +  * #select!
 +
 +Remove from specific indices:
 +
 +  * #delete_at (a given index)
 +  * #pop
 +  * #shift (remove from beginning)
 +  * #slice!
 +
 +Simplify an array:
 +
 +  * #compact (remove nil)
 +  * #compact!
 +  * #uniq
 +  * #uniq!
 +
 +===== Transformation =====
 +
 +These are distinguished from the methods in "​Deletion"​ in that they somehow keep all elements of the array or at least the number of elements.
 +
 +Element by element conversions:​
 +
 +  * #collect
 +  * #collect!
 +  * #map
 +  * #map!
 +
 +Standard conversions:​
 +
 +  * #to_a
 +  * #to_ary
 +  * #to_s
 +
 +Permutations:​
 +
 +  * #reverse
 +  * #reverse!
 +  * #rotate
 +  * #rotate!
 +  * #shuffle
 +  * #shuffle!
 +  * #sort
 +  * #sort!
 +  * #sort_by!
 +
 +Others:
 +
 +  * #* (multiply by integer, repeats array n times)
 +  * #fill (with an object, in a range, the element may be transformed with a block)
 +  * #flatten
 +  * #flatten!
 +  * #join (returns a string)
 +  * #pack (deserves an explanation... why does it exist?)
 +  * #transpose (why does this exist? why not other matrix operations?​)
 +
 +===== Combination with other arrays =====
 +  * #& (set intersection)
 +  * #+ (concatenate and return new)
 +  * #- (array difference)
 +  * #| (set union)
 +  * #zip
 +
 +===== Combinatorics =====
 +  * #​combination
 +  * #​permutation
 +  * #product
 +  * #​repeated_combination
 +  * #​repeated_permutation
 +
 +===== Comparison =====
 +
 +  * #<=>
 +  * #==
 +  * #eql?
 +
 +===== Others, not classified =====
 +
 +  * ::[]
 +  * ::new
 +  * ::​try_convert
 +  * #​initialize_copy