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])