Michael Hwang
3 min readJul 11, 2019

--

Note: Ruby Core Methods of Array (2.6.3)

Arrays are ordered, integer-indexed collection of any object.
Assume ary = Array.new([1,2,3,4]) = [1,2,3,4]

Operator

& = and
| = or
<=> compare

Getting Element by Index:

Array.at(index) — input index number give particular data
ary.at(0)=ary[0]=1
# Array.fetch — Works same as “at”, but return “Error” if index out of bound while at will return nil

Array.index(element) — give element return its index

Array.first , Array.last — return first(0) and last (-1) element

Array.take(n) e— Get First n element
Array.drop(n) — Get all other element after n

Array.dig(n1,n2…) — Return value from nested array while n represent index in each level.
# [[1,[2,3]]].dig(0,1,1) = 3

Get Information of array

Array.length = Array.size = Array.count : Get numbers of element in array
# Count can add counting cirtera with block

Array.empty? — Check whether array is empty

Array.include?(element) — Check whether array include given element

Adding

Array.push(element) = Array <<element — Add new element as latest
# Array.append do the same

Array.concat(other array) — Add new array at last
Array + other array — Create new array and add other array at last

Array.unshift(element) — -Add new element at first and shift other back

Array.insert(index,*element) — Add one or more element at index

Remove

# Most methods here will effect origin array
#Except compact and uniq unless you use “!” version

Array.pop — delete the last element

Array.shift — return first element and remove from array

Array.delete_at(index) — delete specific element

Array.delete(element) — delete all that element(can be more than one)

Array.compact — remove all nil

Array.uniq — return unique element

Change

Array.reverse — Reverse the array

Array.sort {a, b a<=>b} — Sort the element with given criteria and Create new array.
# a<=>b is ascending(default), b<=>a is descending
# Can be replaced by other criteria, such as comparing the length of element: a.length<=>b.length

Array.rotate(n) —Rotating and set n index as the beginning of new array

Array.fill

Array.replace(another array) — replace all the element with the argument

Iterating

Array.each {}— Iterating and yield each element to block
Array.each_index — Same as each but run with index
Array.reverse_index — Iterating from back to front

Array.map{}— similar to each, but will create a new array in the end
# Array.collect do the same thing

Selecting/Filtering

Array.select{} — Return an array with selected element (criteria is in block)
#Array.keep_if — Similar to select but effect origin array

Array.reject{} =Array.drop_while— Return an array with unselected element
#Array.delete_if — Similar to reject but effect origin array

Array.difference(other array(s)) — compare and return array include element only in origin array. Can also use “-” to represent this function.
EX:[1,1,2,3,3,4,5] — [1,5,6] = [2,3,3,4]
## It does not work in my pc at this point. Wait for more inspection.

Array.max/min {|a,b| a<=>b} — Similar to #sort, find the max/min element (n th if input) with give criteria

Array.assco(element) — Return a sub-array whose first element is matched
Array.rassco(element) — Second element version of Array#assco

Array.flatten — Create a one-dimension array of given nested array.
# Can input level argument to decide to flatten the array up to which level.

Statistic method

Array.sample — Randomly select 1 or n (if given) element from array

Array.shuffle — Randomly reorder the array

Array.product(other array(s)) — Return array of all combination of all elements of all arrays

Array.permutation(n) — Yield all permutation of n length element (nPm)
# In detail, it returns an Enumerator object, so should use to_a to switch it to an array.

Array.repeated_permutation(n) — Yield all permutation of n length element(nHm)
# Same as permutation, require to apply to_a to get array

Array.combination(n) — Yield all combination of n length element(nCm)

Other

Array.join(separator) —Return string of all elements combined with a separator

Array.transpose —Transpose the matrix’s row and column

--

--