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.
Hash.each_value
Iterate over each value in the hash.
Hash.empty?
Check if the hash is empty.
Hash.size or length
Return the number of key-value pairs in the hash.
Hash.keys
Return an array containing all keys in the hash.
Hash.values
Return an array containing all values in the hash.
Hash.has_key?(key) or key?(key)
Check if the hash contains a specific key.
Hash.has_value?(value)
Check if the hash contains a specific value.
Hash.transform_keys
Return a new hash with keys transformed by the given block.
Hash.transform_values
Return a new hash with values transformed by the given block.
Hash.invert
Return a new hash with keys and values swapped.
Hash.merge(other_hash)
Merge another hash into the current hash, returning a new hash.
Hash.update(other_hash)
Merge another hash into the current hash, modifying it in place.
Hash[“value”]
Retrieve the value associated with a given key
String +
Concatenates two strings.
String «
Appends the contents of the given string to the original string.
{String}
Allows you to embed Ruby expressions within strings.
String.length or .size
Returns the length of the string.
String[i]
Returns the character at the specified index.
String.slice
Returns the substring starting at the given index.
String.upcase
Converts the string to uppercase.
String.downcase
Converts the string to lowercase.
String.capitalize
Converts the first character to uppercase.
String.swapcase
Swaps the case of each character.
String == String
Checks if two strings are equal.
String.casecmp
Performs a case-insensitive comparison.
String.include?
Checks if the string contains a substring.
String.index
Returns the index of the first occurrence of a substring.
String.scan
Returns an array of all occurrences of a pattern.
String.slice
Returns a substring based on the given index or range.
String.split
Splits the string into an array of substrings based on a delimiter.
String.partition
Splits the string into three parts based on a delimiter.
String.gsub
Replaces all occurrences of a pattern with a specified string.
String.sub
Replaces the first occurrence of a pattern with a specified string.
String.insert
Inserts a substring at a specified index.
String.strip
Removes leading and trailing whitespace.
String.lstrip
Removes leading whitespace.
String.rstrip
Removes trailing whitespace.
String.center
Centers the string within a specified width.
String.ljust
Left-justifies the string within a specified width.
String.rjust
Right-justifies the string within a specified width.
String.to_i
Converts the string to an integer.
String.to_f
Converts the string to a floating-point number.
String.to_sym
Converts the string to a symbol.
String,encode
Changes the encoding of the string.