Ruby Cheatsheet
51 essential methods for coding interviews
Showing 51 of 51 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
push / << | array.push(item) or array << item | Adds one or more elements to the end of the array | O(1) amortized | essential |
pop | array.pop or array.pop(n) | Removes and returns the last element(s) from the array | O(1) for single, O(n) for multiple | essential |
shift | array.shift or array.shift(n) | Removes and returns the first element(s) from the array | O(n) | essential |
first / last | array.first or array.first(n) / array.last or array.last(n) | Returns the first/last element(s) without modifying the array | O(1) for single, O(n) for multiple | essential |
flatten | array.flatten or array.flatten(depth) | Returns a new array that is a one-dimensional flattening | O(n) | essential |
uniq | array.uniq or array.uniq { |item| block } | Returns a new array with duplicate values removed | O(n) | essential |
reverse | array.reverse | Returns a new array with elements in reverse order | O(n) | essential |
sort | array.sort or array.sort { |a, b| block } | Returns a new sorted array | O(n log n) | essential |
sort_by | array.sort_by { |item| key } | Sorts by the value returned from the block | O(n log n) | essential |
include? | array.include?(item) | Returns true if the array contains the given item | O(n) | essential |
index / find_index | array.index(item) or array.index { |x| block } | Returns the index of the first matching element, or nil | O(n) | essential |
slice / [] | array[index] or array[start, length] or array[range] | Access elements or subarrays by index or range | O(1) for single, O(k) for range of k elements | essential |
each | collection.each { |item| block } | Iterates over each element, returns the original collection | O(n) | essential |
each_with_index | collection.each_with_index { |item, index| block } | Iterates with both element and its index | O(n) | essential |
map / collect | collection.map { |item| block } | Returns a new array with block results for each element | O(n) | essential |
select / filter | collection.select { |item| condition } | Returns array of elements for which block returns truthy | O(n) | essential |
reject | collection.reject { |item| condition } | Returns array of elements for which block returns falsy | O(n) | essential |
reduce / inject | collection.reduce(initial) { |acc, item| block } | Combines all elements by applying binary operation | O(n) | essential |
find / detect | collection.find { |item| condition } | Returns the first element for which block returns truthy | O(n) | essential |
any? / all? / none? | collection.any? { |item| condition } | Tests if any/all/none elements satisfy the condition | O(n) | essential |
count | collection.count or collection.count(item) or collection.count { |x| block } | Returns count of elements, optionally matching condition | O(n) | essential |
tally | collection.tally | Returns hash counting occurrences of each element | O(n) | essential |
group_by | collection.group_by { |item| key } | Groups elements into hash by block result | O(n) | essential |
min / max | collection.min or collection.min(n) or collection.min { |a, b| block } | Returns minimum/maximum element(s) | O(n) | essential |
min_by / max_by | collection.min_by { |item| value } | Returns element with minimum/maximum block value | O(n) | essential |
sum | collection.sum or collection.sum(init) { |item| block } | Returns sum of elements | O(n) | essential |
split | string.split(delimiter) or string.split(regex) | Splits string into array by delimiter | O(n) | essential |
join | array.join(separator) | Joins array elements into a string | O(n) | essential |
chars | string.chars | Returns array of characters | O(n) | essential |
gsub | string.gsub(pattern, replacement) | Returns string with all occurrences replaced | O(n) | essential |
downcase / upcase | string.downcase / string.upcase | Returns string with all characters lowercased/uppercased | O(n) | essential |
keys / values | hash.keys / hash.values | Returns array of all keys/values | O(n) | essential |
key? / has_key? / include? | hash.key?(key) | Returns true if hash contains the key | O(1) average | essential |
default / default= | hash.default = value or Hash.new(default) | Sets default value for missing keys | O(1) | essential |
Set.new | require "set"
Set.new(array) | Creates a Set from an array for O(1) lookups | O(n) to create | essential |
Set operations | set1 & set2 (intersection), set1 | set2 (union), set1 - set2 (difference) | Set operations for combining or comparing sets | O(min(n, m)) | essential |
Range | (start..end) or (start...end) | Creates a range; .. is inclusive, ... excludes end | O(1) to create | essential |
times / upto / downto | n.times { |i| block } / start.upto(end) / start.downto(end) | Integer-based iteration methods | O(n) | essential |
unshift | array.unshift(item) | Adds one or more elements to the beginning of the array | O(n) | common |
compact | array.compact | Returns a new array with all nil values removed | O(n) | common |
take / drop | array.take(n) / array.drop(n) | Returns first n elements / returns array without first n elements | O(n) | common |
zip | array.zip(other_array, ...) | Combines arrays element-wise into arrays of arrays | O(n) | common |
find_all | collection.find_all { |item| condition } | Alias for select; returns all matching elements | O(n) | common |
partition | collection.partition { |item| condition } | Splits into two arrays: truthy and falsy | O(n) | common |
strip | string.strip | Returns string with leading/trailing whitespace removed | O(n) | common |
start_with? / end_with? | string.start_with?(prefix) / string.end_with?(suffix) | Tests if string starts/ends with given substring | O(m) where m is prefix/suffix length | common |
Hash[] / to_h | Hash[array] or array.to_h | Creates hash from array of key-value pairs | O(n) | common |
fetch | hash.fetch(key, default) or hash.fetch(key) { block } | Gets value for key with explicit missing key handling | O(1) average | common |
merge | hash.merge(other_hash) { |key, old, new| block } | Returns new hash combining both hashes | O(n) | common |
transform_values | hash.transform_values { |v| block } | Returns new hash with values transformed by block | O(n) | common |
rotate | array.rotate(count = 1) | Returns new array rotated by count positions | O(n) | useful |