Ruby Cheatsheet

761




Basics

Sample program

puts "Hello, World!"
  • puts : Used to print the given text to the console.
  • # : Single line Comment
  • =begin

Multi
line
comment
=end

Variables

1. Global variables

Begins with $ and will have global scope

$var_scope = "GLOBAL"

2. Local variables

Begins with a lowercase letter or underscore (_) and will have scope limited to block of it's initialization.

_greeting = "Hello"
age = 21

3. Instance variables

Belongs to one instance of a class and starts with a @ sign.

@name = "OneCompiler"

4. Class variables

Belongs to the entire class and starts with @@.

@@id = 111

Operators

TypeOperators
Arithmetic Operators+ , - , * , / , % , **
Comparision Operators== , != , > , >= , < , <= , <=>, .eql?, ===, equal?
Bitwise Operators& , ^ , | , ^ , ~ , << , >>
Logical Operators&& , || , ! , and , or, not
Assignment Operators= , += , -= , *= , /= , %= , **=
Ternary Operators? :
Range Operators.. , ...
Special Operatorsdefined?, . , ::

Arrays

Array is a collection of data items and they no need to be of same type.

array_name = Array.new(size)
# or
array_name = Array.new

Example

arr = [ "Good", "morning", 9, 5.32 , true]

Hashes

Hashes represents key-value pairs. => is used to assign value to it's key.

hsh = nationalGame = { "Australia" => "Cricket", "Japan" => "Wrestling", "NewZealand" => "Rugby","USA" => "Baseball"}

Conditional Statements

1. If

if conditional-expression 
    #code
end

2. If-else

if conditional-expression
    #code
else 
    #code
end

3. Nested If-else

if conditional-expression
    #code
elsif conditional-expression
    #code
else 
    #code
end

4. Case

case exp
[when exp [then]
   #code ]...
[else
   #code ]
end

Loops

1. For

for variable [, variable ...] in range [do]
   #code
end

2. While

while conditional-expression [do]
   #code
end

3. Until

until conditional-expression [do]
   #code
end

Methods

def methodName [( [arg [= default]]...[, * arg [, &expr ]])]
   #code  
end 

Example

def sum(x , y)
   sum = x+y
   puts "Sum: #{sum}"
end
sum(10,20) # passing parameters to method sum