Skip to content

Ruby Methods

String Methods

A new string is returned, unless a bang (!) is added after the method (for example, .split!) which mutates the original string.

.strip removes the leading and trailing whitespace on strings
.chomp removes the last character in a string, only if it's a carriage return or new line
.upcase makes all characters in a string UPCASE
.downcase makes all characters in a string DOWNCASE
.class describes the class of an object
.object_id returns the integar identifier for an object
.length returns the number of characters in a string (incl. whitespace)
.count counts how many time a specific character is found in string - for example, "HELLO".count('L')
.insert inserts a string into another string before a given index - for example, "Hello".insert(3, "hi5") -> Helhi5lo
.capitalise makes the first letter in a string uppercase and the rest downcase
.reverse reverses the order of characters in a string - for example, "Hello World!".reverse -> "!dlroW olleH"
.split takes string and splits it into an array, then returns the array default method splits based on whitespace, unless different separator provided ("" to split by character; " " to split by word)
.chop removes the last character of the string - for example, "Batman".chop -> "Batma"
.pop removes the last letter in a string and returns it - for example, "Linda".pop would return "a"
.include?("STRING") to test if a string is included in another
.index("STRING") looks for partial words, returns index where the start of this string is found
to_s converts data type to string
to_i convert data type to integar
to_f convert data type to float
to_sym convert data type to symbol
<< string concatenation
.each_char returns an array of characters in a string
.gsub to replace text in a string: for example, for string = "We have many dogs", method would be string.gsub("dogs", "cats")

Sub-String Methods

string = "abc123" Below examples use this string
string[x,y] x = starting index, y = number of characters you want - for example string[3,3] -> 123
string[0..-2] get characters within a range - first index is starting, second is ending index (inclusive)
(1..10).to_a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
('A'..'Z').to_a creates an array of alphabetical letters - for example ["A", "B", "C", "D", "E", "F"]

Number Methods

.abs returns the absolute (positive) version of an integar/float
.round rounds float to a given precisio in decimal digits, default is 0 - for example, .round(2) -> two decimal places

Array Methods

.join takes an array of strings and joins them into a single string, can pass a character separator as an argument
.length tallies the number of elements inside an array
.first accesses the first element of the array
.last accesses the last element of the array
.take returns the first 'n' elements of the array - for example, arr = [0, 1, 2, 3, 4], the function and result would be arr.take(3) -> [0, 1, 2]
.drop returns the elements after 'n' elements of an array
.pop permanently remove the last element of an array
.shift permanently remove the first element of an array, returns element
.push to add an element to the end of an array - for example, arr.push(99) -> [0, 1, 2, 3, 4, 99]
.unshift add an element to the beginning of an array arr.unshift(1) -> [1, 2, 3]
.delete removes a specified element from an array permanently
.delete_at permanently remove an element of an array from a specified index
.reverse reverses the array but does not mutate it
.select iterates over an array and returns a new array that includes values that return true
.is_a? Integar iterates through an array to check Class type.

Conversion Functions

.to_i convert to integer
.to_f convert to float
.to_s convert to string
.to_sym convert to symbol
.to_a convert to array