Instance Methods Flashcards
join
Fn: Return a string which is the values in the array converted to strings and concatenated.
Returns: string
Syntax: my_array.join(separator)
#shuffle #shuffle!
Fn: put the array items in random order
Returns: Array (!) or new array
each
Function: Iterates through the array, applying the block once to each item
Returns: The original array
Syntax: my_array.each {|item| block}
(Returns Enumerator if no block is given)
#map #map! #collect #collect!
Fn: Transform an array by iterating through and passing each element as a parameter.
Returns: The first method returns a new array.
The second, with the bang suffix, mutates the original array in place and returns it.
Syntax: same as #each
(Returns Enumerator if no block is given)
#uniq #uniq!
Fn: removes duplicates. Use return value of block for comparison
Returns: array (!) or new array
Syntax: my_array.uniq{|item|}
#sort_by #sort_by!
Fn: sort using a set of keys generated by mapping the values in the array through the given block
Returns: a new array (or mutates array in place when !)
Syntax: my_array.sort_by{|object| block}
keep_if
Fn: evaluates the block and deletes every element from the array for which it evaluates to false
Returns: the original array
Syntax: same as #delete_if
each_index
Function: Like each, but passes the index of the element instead of the element itself
Returns: the original array
Syntax: my_array.each_index{|item| block}
#take #drop
Fn: take is like first(n). Drop returns the elements after the first n (i.e. the remaining ones)
Returns: new array
Syntax: my_array.take(n)
delete
Fn: destructively remove an element from the array matching obj
Returns: the last deleted item
Syntax: my_array.delete(obj)
values_at
Fn: return the values at the given indices
Returns: new array
Syntax: my_array.values_at(int1, in2… or a range)
+ / #concat
Fn: concatenate two arrays
Returns: a new array
Syntax: my_array + other_array
#rotate #rotate!
Fn: rotates the array elements so that count is the first element
Returns: Array (!) or new array
Syntax: my_array.rotate(count=n)
delete_at
Fn: destructively remove the object found at a given index
Returns: the deleted object or nil if not found
Syntax: my_array.delete_at(idx)
#flatten #flatten!
Fn: make a one dimensional array
Returns: array (!) or a new array
Syntax: my_array.flatten([level])
#difference [ ]-
Makes a new array that is a copy of the receiver, minus the duplicates, preserving order
#first #last
Fn: return the first/last (or first/last n) elements from the array
Returns: either the element or an array of n elements
Syntax: my_array.first(n)
#slice #slice!
Fn: return a sub-array starting at idx, for a given length/range (optional)
Returns: the array (!) or a new array
Syntax: my_array.slice(idx,length/range) or nil
insert
Fn: Inserts object after idx
Returns: the array
Syntax: my_array.insert(idx, obj)
to_s / #inspect
Fn: returns the string interpretation of the array
Returns: a string
length / #size
Returns number of elements in array
#empty? #include?(obj)
These return Boolean values after checking if the array is empty or if it contains (obj)
#union |
Joins arrays, excluding duplicates, preserving order
transpose
Given an array of arrays transposes the rows and columns
#shift #pop
Fn: remove the first/last element from the array
Returns: the removed element (or nil)
Syntax: my_array.shift
#select #select! #filter #filter!
Fn: iterates through the array, passing each element as parameter to the block and returning it if the block evaluates to ‘true’
Returns: a new array containing the selected results (the methods with a bang suffix mutate the array in place)
Syntax: same as #each
(Returns Enumerator if no block is given)
#reverse #reverse!
Fn: put the array elements in reverse order
Returns: the array(!) or a new array
sample
Fn: Choose a random element from the array
See docs for further usage
fetch(idx)
Fn: This is the most basic usage which returns the element at idx
#sort #sort!
Fn: comparisons for the sort are done using the <=> operator or an optional code block… the block implements a comparison of a & b
Returns: a new array
Syntax: my_array.sort{|a, b| block}
#unshift #push / #<
Fn: prepend/append the given object onto the array
Returns: the array
Syntax: my_array.unshift(obj)
take_while
Fn: Passes elements to the block until it returns false/nil
Returns: a new array
Syntax: my_array.take_while{|obj| block }
delete_if
Fn: Delete from the array if the block evaluates to true
Returns: original array
Syntax: my_array.delete_if{|item| block}