Learn Ruby From A to Z


Hello Folks,

Today we are going to learn about one of the popular Programming language Ruby. Here you are going to learn everything about the basic ruby stuffs. You can use this as your documentation for learning Ruby.

Instead of watching videos about Ruby I urge you guys to follow the documentation or article. I have made this article with the sole purpose that you don’t have to visit other website or watch the videos to learn the Ruby programming language basics.

So let’s get started…

The most fundamental thing that you need to learn to get started with any programming languages are :

1. Give Output and take Input
2. String
3. Array
4. Class and Object
5. Methods
6. Loops

If you are able to grasp these concepts of any programming language then you are good to go. So we are going to follow the same pattern in learning Ruby on rails.

1. Give Output and take Input

To print the output to the screen there are two ways and they are:

i. puts
This is used to print the statement or line and moves onto the next line after its completion

ii. print
This is used to print the statement or line and doesnot move onto the next line after its completion instead it prints on the same line after it end.

And to take input in Ruby what you can do is

puts "What is your name?"
puts "Hello #{name}, How are you"

Hello Sagar
, How are you

This will print like this instead of printing out in same line and for us to obtain it without the the n we need to get it chomped

puts "What is your name?"
puts "Hello #{name}, How are you"

Output will be:
Hello Sagar, How are you

2. String

String are the combination of characters.
To create a string you need to put the characters inside the enclosed single code ”

str='Hello this is string'

Here str is the string. Also String is an inbuilt method for the creation of string and you can create string with this way too.

str1=String.new("Hello WOrld")
puts str1

To Find the length of string

puts str.length

For iterating through the characters inside the string using for loop


for i in (0..str1.length)
  puts str1[i]

For splitting the words in strings after every spacing we use

str1="Hello World k ca"
puts str1.split

3. Array

There are many ways to create array in Ruby. One of the method is to directly assign an empty array enclosed inside [ ] bracket . And another way is to do it using Array method using new initialize keyword

# names=[]
puts names

Also to insert elements inside array you can either use .push or [index].

TO find the length of Array also we use .length


4. Class and Object

To create class in Ruby we start with the class keyword followed by its name and the first letter of its name is in uppercase. And it ends with the keyword end

class Vehicle


This is the way we create class.

Now to create variable in class

class Vehicle

  def vehicle_speed()


puts car1.vehicle_speed()

And during the initialization phase of the creation of class the varible name contains single @ and the variables inside the class gets two @

class Vehicle

  def vehicle_speed()

  def initialize(name,country)

  def define
    puts "The Name of Car is #{@car_name} and it was manufactured in #{@car_country}"




5. Method or Function

Method or function is written inside the the def followed by its name in lowercase and ends with the end keyword

Objects is created to access the variable, or method of inside the class. Object is the instance of class. Objects can be created by following a class name with the dot operator followed by the new keyword.

def greet(name)
  puts "Hello #{name}"


6. Loops

There are many types of loop used inside the Ruby some of them are:

i. For Loop

Syntax for the use of for loop inside the ruby are:

for i in (0..10)
  puts i

ii. While do Loop
Syntax for the use of while do loop inside the ruby are:


while $i<$num do
  puts $i

iii. Each do loop

Syntax for the use of each do loop inside the ruby are:

(0..5).each do |i|
  puts i

These are the basics that you must know before diving deep into the ruby programming language. Hope you continue your learning journey. Sayonara <3

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

Optimizing Docker Images with Multi-Stage Builds and Distroless Approach

Next Post

The Best Marketing Jobs and Careers for You to Pursue in 2023 (BIR NNP)

Related Posts