Show Menu
Cheatography

Ruby Programming Cheat Sheet (DRAFT) by

General overview of methods and syntax for the Ruby programming language.

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

Getting Help

ruby-l­ang.org
Primary resource for Ruby "Ruby Websit­e"
ruby-d­oc.org
Official docume­ntation for Ruby.

Running Single Commands

ruby -e 'puts 123'
ruby -e 'print 123'
ruby -e 'puts "­Hel­lo".r­ev­erse'

Creating a Comment

# Single line comment

Accessing IRB

irb
irb --simp­le-­prompt

Compar­iso­n/Logic Operators

Equals
==
Less than
<
Greater than
>
Less than, equal to
<=
Greater than, equal to
>=
Not
!
Not Equal
!=
And
&&
Or
||
Returns true/false

Condit­ionals: if, else, elsif

x = 1

if x == 1
 ­ ­ ­ ­ puts "Text here"
end

if x == 2
 ­ ­ ­ ­ puts "Text here"
else
 ­ ­ ­ ­ puts "Text here"
end

if x == 2
 ­ ­ ­ ­ puts "Text here"
elsif x == 1
 ­ ­ ­ ­ puts "Text here"
else
 ­ ­ ­ ­ puts "Text here"
end

Condit­ionals: unless, case

x = 1

unless x == 2
 ­ ­ ­ ­ puts "This runs if the above boolean is false."­
end

case
 ­ ­ ­ ­ when boolean
 ­ ­ ­ ­ ­ ­ ­ ­ ­ puts "Text here"
 ­ ­ ­ ­ when boolean
 ­ ­ ­ ­ ­ ­ ­ ­ ­ puts "Text here"
 ­ ­ ­ ­ else
 ­ ­ ­ ­ ­ ­ ­ ­ ­ puts "Text here"
 ­ ­ ­ ­ end

case test_value
 ­ ­ ­ ­ when value
 ­ ­ ­ ­ ­ ­ ­ ­ ­ puts "Text here"
 ­ ­ ­ ­ when value
 ­ ­ ­ ­ ­ ­ ­ ­ ­ puts "Text here"
 ­ ­ ­ ­ else
 ­ ­ ­ ­ ­ ­ ­ ­ ­ puts "Text here"
 ­ ­ ­ ­ end

Inline Condit­ional

puts "­tes­t" if name == "­Fra­nk"

Ternary Operator

x = boolean ? "test 1" : "test 2"
This will assign one of the 2 values based on the boolean result.

OR/OR-­EQUALS Operator

x = y || z
If y has a value set x equal to y else set it equal to z.
x ||= y
If x has a value, nothing happens. If it does not then set x to the value of y.

Loops

x = 0
loop do
 ­ ­ ­ ­ x+=2
 ­ ­ ­ ­ ­break if x >= 20
 ­ ­ ­ ­ puts x
end
You can use the following within a loop
break  ­ ­ ­Ter­minate the whole loop
next  ­ ­ Jump to the next loop
redo  ­ ­ Redo this loop
retry  ­ ­ ­Start the whole loop over again

Loops: while

x = 0
while x < 20
 ­ ­ ­ ­ x += 2
 ­ ­ ­ ­ puts x
end

# You can also use the inline version
x = 0
puts x += 2 while x < 100

Loops: until

y = 23245
until y <= 1
 ­ ­ ­ ­ puts y /=2
end

# You can also use the inline version
y = 23245
puts y /= 2 until y <= 1

Loops: for

fruits = ['banana', 'apple', 'pear']

for fruits in fruits
 ­ ­ ­ ­ puts fruit.c­ap­tialize
end

Iterators

5.times { puts "­Hel­lo" }
1.upto(5) { puts "­Hel­lo" }
5.down­to(1) { puts "­Hel­lo" }
(1..5).each { puts "­Hel­lo" }
array.each { |num| puts num * 20 }
You can use do and end inplace of { }
 

Variable Scopes

Global Variable
$variable = "­Tes­t"
Class Variable
@@variable = "­Tes­t"
Instance Variable
@variable = "­Tes­t"
Local Variable
variable = "­Tes­t"
Block Variable
variable = "­Tes­t"

Integers

1234.class
This will tell you what class the Integer object belongs to.
10.2.to_i
Will convert number to integer.
Stored in 2 ways: Fixnum and Bignum

Floats

12345.1­0.r­ound
Rounds the float to integer.
12345.to_f
Converts a integer to a float.
12345.1­0.f­loor
Rounds down to whole number.
12345.1­0.ceil
Rounds up to whole number.

String Methods

"­Hel­lo".r­everse
"­Hel­lo".c­ap­italize
"­Hel­lo".d­ow­ncase
"­Hel­lo".u­pcase
"­Hel­lo".l­ength
"­Hel­lo".u­pc­ase.re­verse
Strings can be in single or double quotes. Ruby will always return them in double quotes.

Constants

Similar to variables, not true objects.

A constant should not change unlike a variable.

Define constants in all CAPS
TEST = 2

Anything that begins with a capital letter at the beginning is considered a constant.

If you try to change the value of a constant, it will display a warning, but will still change the value.

Boolean Methods

z.nil?
Will check if the variable z is == to nil
2.betw­een­?(1,4)
Will check if the number 2 is between 1 and 4
[1,2,3­].e­mpty?
Will return true/false if its empty.
[1,2,3­].i­ncl­ude?(2)
Returns true/false if the number 2 exists in array.
{‘a’ => 1, ‘b’ => 2}.has­_ke­y?(‘a’)
Returns true/false if the key exits.
{‘a’ => 1, ‘b’ => 2}.has­_va­lue?(2)
Will return true/false if the value exits.
 

Arrays

data_set = []
Sets an empty array, and also clears out existing array
data_set = [“a”, “b”, “c”]
Sets an array with data
data_s­et[1]
Returns data from the defined positi­oned.
data_s­et[0] = “d”
Sets the value of the element with key 0 to d
data_set << “e”
Appends the data to the array
data_s­et[1] = nil
Removes data from an array
data_s­et.c­lear
Clears out an array

Array Methods

array.i­nspect
Will return a string repres­ent­ation of the array.
array.to_s
Joins array elements together.
array.j­oi­n(",­")
Will implode the array by comma.
x = “1,2,3­,4,5”; y = x.spli­t(‘,’)
This will return an array, separating each value by comma.
array.sort
Will sort your array asc order
array.uniq
Will return an array with no duplicates
array.u­niq!
Will update the array with the new version in place.
array.d­el­ete­_at(2)
Will delete the element based on key and return the value that it it deleted.
array.d­el­ete(4)
Will delete the element based on value
array.p­ush(4)
Will add to an array - last position
array.pop
Will remove the last element from the array
array.s­hift
Will remove the first element from the array
array.u­ns­hift(1)
Will put the value to the front of the array
array + [9,10,­11,12]
Will take first array and add these other values from the second array to it.
array - [9,10]
Willl search and remove the values 9,10

Hashes

mixed = {1 => [‘a’, ‘b’, ‘c’], ‘hello’ => ‘world’, [10,20] => ‘top’}
You can have mixed values in hashes.
mixed.keys
Returns all of the keys
mixed.v­alues
Returns all of the values
mixed.l­ength
Returns the length of the hash
mixed.size
Returns the length of the hash
mixed.to_a
Converts the hash to an array
mixed.c­lear
Will return an empty hash
mixed = {}
Will return an empty hash
mixed.k­ey­('w­orld')
Will return the key of the hash value.
mixed[­'test'] = 'value'
Will add/set value to hash.
mixed[­[10­,20]]
Returns the value for the hash key which is [10,20]

Symbols

:test
Prefixed with a colon and stored in memory once where as a string is stored in memory each time.
hash = {:firs­t_name => “Frank”, :last_name => “Perez”}
Works well with hashes.
hash[:­fir­st_­name]
You will need to reference the symbol from the hash like such.
A label used to identify a piece of data.

Ranges

1..10
Inclusive Range
1...10
Exclusive Range
(1..10­).to_a
Converts the range to an array.
(1..10­).class
Will let you know that its a range.
x = 1..10
Sets a range to the variable x.
x.begin
Returns the first number.
x.first
Returns the first number.
x.end
Returns the last number.
x.last
Returns the last number.
z = [*x]
Using the splat operator *, you can assign the range as an array.
y = 1...10; y.incl­ude­?(10)
Returns false, as it is not included in the range.
alpha = ‘a’..’m’
Creates an inclusive range of letters.
alpha.i­nc­lud­e?(‘g’)
Will return true, as it exists in the range.
[*alpha]
Will return all the letters in the range, array format, shorthand.
Inclusive ranges include all numbers in a range where exclusive ranges excludes the last number.