Learn Ruby From A to Z
Published: 2024-01-07 09:03:22

**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?"
name=gets
puts "Hello #{name}, How are you"

Output:
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?"
name=gets.chomp
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

```
str='Hellostring'
puts str.length

```

For iterating through the characters inside the string using for loop

```
str1="Hello"

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


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=Array.new()
# names=[]
names.push("Sagar")
names.push("Kattel")
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 

```
names.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
  
end

```
This is the way we create class.

Now to create variable in class

```
class Vehicle
  @@speed=100
  
  def vehicle_speed()
    @@speed
  end
end

car1=Vehicle.new

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
  @@speed=100
  
  def vehicle_speed()
    @@speed
  end
  
  def initialize(name,country)
    @car_name=name
    @car_country=country
  end
  
  def define
    puts "The Name of Car is #{@car_name} and it was manufactured in #{@car_country}"
  end
  
end


mercedes=Vehicle.new("Mercedes","USA")

mercedes.define()
```


**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}"
end

greet("Sagar")
```


**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
end
```

**_ii. While do Loop_**
Syntax for the use of while do loop inside the ruby are:

```

$i=0;
$num=5;

while $i<$num do
  puts $i
  $i+=1
end 
```

**_iii. Each do loop_**

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


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



**These are the basics that you must know before diving deep into the ruby programming language.<3**