Common Ruby Methods Flashcards
Array.each
Iterates over each element in the array
Array.map or collect
Transforms each element of the array based on the provided block and returns a new array with the transformed elements.
Array.select or find_all
Returns a new array containing all elements for which the block returns true.
Array.reject
Returns a new array containing all elements for which the block returns false.
Array.find or detect
Returns the first element for which the block returns true.
Array.include?
Checks if the specified element exists in the array.
Array.first
Returns the first element of the array
Array.last
Returns the last element of the array
Array.empty?
Checks if the array is empty
Array.size or length
Returns the number of elements in the array.
Array.push or «
Adds one or more elements to the end of the array
Array.pop
Removes and returns the last element of the array.
Array.unshift
Adds one or more elements to the beginning of the array.
Array.shift
Removes and returns the first element of the array.
Array.concat
Concatenates one array onto another
Array.join
Combines all elements of the array into a single string.
Array.sort
Sorts the elements of the array.
Array.reverse
Reverses the order of the elements in the array.
Array.uniq
Returns a new array with duplicate elements removed.
Array.compact
Returns a new array with nil elements removed.
Array.filter_map (2.7+)
Returns a new array containing the results of running the block once for every element in the enum, only including the results for which the block returns a truthy value.
Array.inject or reduce
This method is used to combine all elements of a collection (such as an array or a hash) by applying a binary operation, specified by a block or a symbol, repeatedly to each element in the collection.
Hash.fetch(key)
Retrieve the value associated with a given key, or return a default value if the key is not found.
Hash.store(key, value)
Store a value with a given key.
Hash.merge!(other_hash)
Merge another hash into the current hash, modifying it in place.
Hash.delete(key)
Remove a key-value pair from the hash by key.
Hash.clear
Remove all key-value pairs from the hash.
Hash.each
Iterate over each key-value pair in the hash.
Hash.each_key
Iterate over each key in the hash.