Ruby


Types

str

String

"This is a string!"

symbol

int

Integer

64

float

Float (number with decimal point)

64.0

bool

Boolean value (either True or False)

True

complex

Complex number

1j

list

List (collection of data that can be mutated)

["A", 2]

hash

Dictionary (collection of data where each member references a value)

{"A" : 1, "B" : 2}

Arithmetic operators

+

Addition

1+2

Unary positive, makes number positive

+2

-

Subtraction

2-1

Unary negative, makes number negative

-2

*

Multiplication

2*3

/

Division

4/2

%

Modulo (remainder of division)

4%2

**

Indice

4**2

Assignment operators

=

Equals

+=

a+=b is a=a+b

-=

a-=b is a=a-b

*=

a*=b is a=a*b

/=

a/=b is a=a/b

**=

a**=b is a=a**b

%=

a%=b is a=a%b

Comparison operators

==

Is equal to

!=

Is not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

<=>

The spaceship; in a<=>b, returns 0 if a==b, 1 if a>b, and -1 if a<b

===

.eql?

Returns true if both objects have same type and value

equal?

Returns true if both objects have the same object ID

Bitwise operators

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

<<

Arithmetic shift left (shifts all the bits one space to the left, essentially multiplying it by 2)

>>

Logical shift right (shifts all the bits one space to the right, essentially dividing it by 2)

~

Bitwise NOT

Boolean operators

and

AND operator

or

OR operator

not

NOT operator

Identity operators

is

Checks if two objects are the exact same object, not just the same value

is not

Checks if two objects aren't the exact same object, even if they have the same value

Identity operators

is in

Checks if an object is an element of another

is not in

Checks if an object isn't an element of another

Miscellaneous operators

.

Applies method to object

::

References global instance of a variable name

References instance of a variable name within a certain scope

[]

Defines a array
References a array's index

()

Defines a list
Denotes parameters of a function

{}

Defines a hash
Defines the actions of method blocks

Defines a lambda expression

x = lambda { |a| a+1 }

=>

Applies value to key in hash

""

Declares a string

"Hello world!"

''

Declares a string

'Hello world!'

@@

Declares a class static variable

@@x = 2

@

Declares a class instance variable

@x = 2

<

Declares a parent class of a class

class Foo < Bar

..

Creates array including all numbers between the given range (inclusive of those two numbers)

*

The splat; indicates unknown amount of arguments for a parameter and treats them as one list

<:<

Pushes object onto list

Key word

lambda

Used for declaring lambda functions

super

Used for referencing the class' superclass

when

Used for declaring when statement in a case statement

is

Used for testing if two variables are the same

and

Used for applying and logic

or

Used for applying or logic

not

Used for applying not logic

in

Used for referencing objects in an array or string

as

Used for declaring foreign files as a variable

require

Used for importing foreign classes for use in code

return

Used to give a final value at the end of a method

pass

Used to tell compiler to do nothing

continue

Used to tell compiler to complete the next iteration of a loop

break

Used to tell compiler to end the loop

from

Used to import specific methods from class

do

Used for declaring loop

with

Used to handle function with variable for a portion of code

as

Used to handle class or variable with an alias name

raise

Raises an error object

global

Used to declare public variable in a method

del

Deletes an object from memory

true

Represent a true boolean value

false

Represents a false boolean value

self

References the class instance

class

Declares a class

class Foo
 code
end

class Foo < Bar
 code
end

def

Declares a function

def function
 code
end

def function(args)
 code
end

case

Switches logic flow based on the output of an expression

case object
when 1
 code
end

for

Executes code for every object in another object

for object in object
 code
end

.each

Executes code for every object in list

list.each { |object| code }

.times

Executes code n times

number.times { code }

if

Executes code if condition is true

if condition
 code
end

unless

Executes code unless a condition is true

unless condition
 code
end

else

Executes code when if or elif isn't executed

if condition
 code
else
 code
end

elsif

Executes code on true condition if the last if statement was false

if condition
 code
elsif condition
 code
end

begin

Attempts to execute code and jumps to the subsequent except on an error

begin
 code
end

rescue

Executes code on failed begin statement

begin
 code
rescue
 code
end

while

Loops code while a condition is true

while condition
 code
end

until

Loops code until a condition is true

until condition
 code
end

initialize

Constructor of a class (method to make objects of a class)

def initialize(args)
 code
end

Default functions

puts

Prints a message

puts string

Methods

push

Declares a new object to an array

list.push(object)

length

Returns length of an array

list.length

pop

Removes last object from an array

array.pop

Directives

require

Includes foreign file's class

require module

Commenting

#

Used to tell compiler the rest of this line is not to be compiled

#This isn't executed

=begin

Opens a multi line comment

=close

Closes a multi line comment