Show Menu
Cheatography

Ruby Cheat Sheet (DRAFT) by

An Easy Reference for Ruby Developers

This is a draft cheat sheet. It is a work in progress and is not finished yet.

General Remarks about the Language

IRB is an intera­ctive Ruby Shell good for starting out. ($ irb)
Ruby does not need to be compiled (most of the time) since it is interp­reted.
Everything in Ruby is an object.
Ruby variables don't have types - only objects do.
Ruby wants you to omit ()'s and ;'s
Ruby will make you happy so enjoy.

Control Structure

if [conditional] ...
else ... end
---------------------------------
[conditional] ? true block ... : false block ...
---------------------------------
if [conditional] ...
elsif [conditional] ...
else ... end
---------------------------------
while [conditional] ... end
---------------------------------
until [conditional] ... end
---------------------------------
case someVar
when [condition] ...
when [condition] ...
(as many whens as needed)
else ... end
---------------------------------
someVar.each do |x| ... end
someVar.each {|x| ... }
---------------------------------
for each x in someVar do ... end

Variable Classi­fic­ations

variab­le_name
Local Variable
VARIABLE
Constant Variable
@varia­ble­_name
Instance Variable
@@vari­abl­e_name
Class Variable
=
Assignment
Everything is an object so variables do not have explicit data types
 

Strings

.length
Length of the String
.count­(param)
How many times the param appears in the String
.inser­t(pos, param)
Inserts the param in the position of the String
.upcase
Converts all characters to uppercase
.downcase
Converts all characters to lowercase
.swapcase
Converts all uppercase characters lowercase & lowercase to uppercase
.reverse
Reverses the order of the characters
.split
Breakes up a String on whitespace and stores all those strings in an array
.chop
Removes the last character
.strip
Removes all whites­pace, tabs, new lines & carriage returns
.chomp
Removes the last character if it's a new line or carriage return
[start, end]
Returns a substring
.to_i
Converts to integer
+
Concat­enates strings
.index­(po­sition)
Returns the character in the specified position
.clear
Removes all content

Function Structure

def methodName ... end

def methodName (param1, param2) ... end
No Return Statem­ents.
No need for ()'s with no parame­ters.
 

Operators

Arithmetic
Operators
+
Addition
-
Subtra­ction
*
Multip­lic­ation
/
Division
%
Modulus
**
Exponent
Compare
Operators
==
Values Equal?
!=
Values Not Equal?
>
Left op greater than Right op?
<
Left op Less than Right op?
>=
Left op greater than or equal to Right op?
<=
Left op less than or equal to Right op?
<=>
Spaceship: returns 0 if ops are equal, 1 if Left op is greater than Right op and -1 if Left op is less than Right op
===
A case compar­ative for when control structure
.eql?
Values Equal for both type and value?
.equal?
Values are the same object?
Logical
Operators
and, &&
AND
or, ||
OR
not, !
NOT
 

Arrays

array = Array.n­ew­(le­ngt­hParam)
Creates array where each element is NIL
array = [element1, element2, ...]
Creates array with the specified elements
array[­index]
Returns the element value at index
array.l­ength
Returns the size of the array
array.push param <<
Adds the params as separate elements to the end of the array
array.pop
Removes the element from the end
array.u­nshift param
Adds the params as separate elements to the front
array.s­hift
Removes the element from the front
array.r­everse
Reverses the order of elements
array.s­huffle
Randomly shuffles up order of elements
array.sort
Sorts the array of elements
array.i­nc­lude? param
Returns true if the param exists in the array
array.uniq
Returns an array of only the unique elements
array.fill param
Sets all the array elements to the param
array.each {|x| ...}
Iterates over each element in the array
array.e­ac­h_i­nde­x{|i| ...}
iterates over each index in the array
A single array can hold elements of different object types.

Ranges

range = Range.n­ew­(start, end)
Creates a new Range from the starting point to the end point
range = start..end
Creates a range from start to end inclusive
range = start...end
Creates a range from start to end exclusive
range.to_a
Converts a range to an array
range.each
Iterators through each element
range.i­nc­lud­e?(­param)
Returns true if the param exists in the Range
range.last param
Returns the last element. Param can be added to provide more than just the last.
Above each function can be used with (start..end) or (start...end) as well

Hashes

map = Hash.new
Creates an empty hash map
map = Hash.n­ew(­def­ault)
Creates an empty hash map where if key or value cannot be found, default value is returned.
map = Hash["k­ey1­" => value1, "­key­2" => value2, ...]
Creates a hash map with 2 key-value pairs
map = Hash["k­ey1­" => value1, "­key­2" => value2, ...]
Creates a hash map with 2 key-value pairs
map["ke­y3"] = value3
Adds a key-value pair to the map
map.ha­s_key? key
Returns true if the key exists as a key in the hash map
maps.h­as_­value? value
Returns true if the value exists as a value in the hash map
map.fetch key
Returns the value that corres­ponds to the key
map.delete param
Deletes the key-value pair with the key param
map.length
Returns the number of key-value pairs in the hash map
map.keys
Returns an array of all the keys in the hash map
map.values
Returns an array of all the values in the hash map
map.sort
Sorts the keys of the hash map in alphab­etical order
map.in­spect
Returns the current state of the hash map
map.each {|k, v| ... }
Iterates over each key-value pair in the hash map
map.ea­ch_key {|k| ... }
Iterates over each key in the hash map
map.ea­ch_­value {|v| ... }
Iterates over each value in the hash map
map.ea­ch_­value {|v| ... }
Iterates over each value in the hash map

Class Structure

class className1
@instVariable
@@clssVariable
...
attr_accessor :instVariable 
..
        def initialize
        ...
        end
   
        def classMethod1 param
        ...
        end
end

class className2
...
end

Class Details

Initialize
A method that is called internally when .new is called to create the object.
attr_r­eader
Instance variable getter
attr_w­riter
Instance variable setter
attr_a­ccesor
Instance variable getter and setter
classN­ame.new
Method called to create the specified object
No overlo­ading methods in Ruby.
Multiple classes can be written in the same file.