As you already know that learning Ruby language basics is vital for Ruby On Rails development . This Ruby language quick Reference For ruby on rails development will help you going.

So in this Ruby language Quick Guide , we go from absolute basics to intermediate level of Ruby language .

We will cover ruby language syntax and implementation of general programming terms like variables, control statements , loops , functions / methods , and various Object Oriented Programming concepts like classes , objects and packages / modules. We will also take a brief view at file handling with ruby .

So let’s get started !!

We will proceed section wise , following are the sections covered

  1. Ruby Basics
  2. Methods in Ruby
  3. Object Oriented Programing (OOP) In Ruby
  4. File Handling

1. Ruby Basics

“Ruby is a dynamic , object oriented and general purpose programming language”

Note : each and everything in Ruby is OBJECT. Ruby is Open Source .

Hello world :

Now , lets write our first program i.e. obviously ” hello, world!”

puts “Hello , world!”

print “Hello, World!”

we can use either of the 2 methods . But the main difference between using puts and print is , puts use a “\n (new line character ) ” at the end of the output line and print don’t .

commenting code :

single comments : use ‘ # ‘ symbol to create a single line comment

#this is sample single line comments

multiline comment starts with ‘ =begin ‘ and ends with ‘ =end’


sample multiline




variables are ‘ names of storage locations ‘

lets understand with example :


here m is a variable name and m is assigned value 9 . we will learn about assignment in later sections of this guide.

NOTE: Variable name can contain either alpha numeric characters or underscore (_). But must not start with number or capital letter.


A name of storage location just like variable , But variable values can be changed and constant remain , as its name suggest ‘constant /unchanged ‘ through out the execution of program .

Note : constant names start with capital letters.

example :

rubyOnRails is a variable while RubyOnRails is a constant.

Just use a capital letter at beginning and it become a constant.

Data Types:

any program / software / application is ultimately made to handle a data of some sort . Data Type is basically type of a data that a variable can contain.

In ruby , any variable can be of any data type and ruby automatically determine datatype of a variable .

example :

tony= 3000 #int

stark =1.2 #float

ironman=” avenger ” #string


Mathematical Operations :

Ruby supports all the arithmetic operations like

  1. addition (+)
  2. subtraction (-)
  3. multiplication (*)
  4. division (/)
  5. modulo division (%)
  6. exponent operator (**)



c=a+b # addition

c=b-a #subtraction

c=a*b #multiplication

c=a/b #division

c=a%b #modulo devision

c=a**b #this raises 5 to the power of 6

Note : To get float result after division operation , use any one float number .

Note : all operators can be used with floating point numbers also

Assignment :

This one is not the assignment given in college ? . This is assignment operator mentions above in the variable section .

We will check out assignment operator and some techniques of assignment .

assignment operator is used to put value in the variable or constant .

denoted by equal sign (=).

Note : (=) is assignment and (==) is comparison don’t get confused here.

Shorthand Assignment/self assignment

This type of assignment is used to reduce code length .


a+=b # same as a=a+b

likewise all the arithmetic operators can be used with self assignment operator

Parallel Assignment:

Lets have a look at one of the most convinient features in ruby.

example : case 1 : normal assignment




case 2 : parallel assignment


a,b=b,a #number swapping

as you can notice the difference , case 1 takes more efforts than case 2 , While case 2 makes workflow smoother ?

Operator Precedence:

It is importance given to operators.

As shown below, operators are listed from higher to lower order of their precedence .

  1. Exponentiation
  2. Multiplication
  3. Division
  4. Modulus operator
  5. Addition
  6. Subtraction

Note : order of execution can be changed using paranthesis () .

Strings :

Now , we have completed operators , lets learn about strings .

String is nothing but anything enclosed between double or single quotation marks. [ ‘ ‘ or ” “]

Note : always prefer double quotes for creating or manipulating strings , single quotes have many restrictions while operating . many escape sequences and characters can not be used with single quotes. Single quotes also doesn’t support string interpolation , which we will be discussing in next section.


text = “Avengers end game is awesome movie “

String Interpolation :

Here comes the awesomeness of strings . Using string interpolation property of ruby strings , we can embed any value inside a double quoted string using #{} .

Note : while using #{} , please make sure there is no space in # and {.




puts “The sum is #{num1+num2}.”

Here , the part #{num1+num2} will be replaced by actual sum of num1 and num2 while executing.

String concatenation :

While working with actual projects , we often need to join two or more strings together . The same functionality is provided by concatenation .Here we can join 2 or more strings together.


dialog1 = ” what can I do for you ?”


puts dialog1+dialog2 #result : “what can I do for you ? sir !”

In this way you can add multiple strings together and join them.

Note : just make sure you are not adding any number with strings . Although + sign is same in case of numbers and strings , both work differently according to context .

String Repeatition:

We can repeat strings without printing multiple times.


text = “hey!”

puts text*5 #outputs : “hey!hey!hey!hey!hey!”

note : Never ever multiply a string by another string . It just throws error . But I know you are going to do it . so just check it !?

and here we finish our string part .

Input :

For making any program / app/ software interactive , the must have thing is user input .

So let’s see , how we can get user input in ruby language.

To get input , ‘gets’ method is used.



puts text #this outputs user entered input

Now let us see , how we can modify functionality of gets according to our custom input requirements.

  1. chomp – by default gets method return string with a \n character at end so chomp is used to get input without the new line character
  2. .to_i –> integer number input
  3. .to_f –>floating point number input
  4. .to_s –>string type input
  5. .to_a –> array input
  6. .to_r –>fractional number input
  7. .to_h –> hash input (we will discuss hashesh in later part )
  8. .to_c –>complex number input
  9. .to_sym –> symbol input


puts ” Enter Name:”


puts “Welcome , #{name}”

here we have used chomp to remove \n character from the end of input . In similar fashion , we can use .to_i to get integer number input and other types of inputs also.

Booleans :

This is special data type where , value of variable can only be either True (1)? or false (0) ? .

example :



Note : Its general rule that boolean types of variable names should be like a verbal question , that should answer either true or false.

In above example, variable like isAlive can be used to create a game app.

Comparison Operators :(==, !=,<,>,<=,>=)

These operators are used to compare two entities.

It returns true if condtition satisfies and false is not.

Another method to compare entities is to use .eql method .




puts x==y #outputs false

example 2:

puts 3.eql?(3.0) #false (.eql method compare data types)

we will continue with Ruby language quick Reference For ruby on rails development in next blog post, Stay tuned for more updates.