Complete Tour Guide of Programming Language Ruby – Episode 2

Previously:

In episode one, we got to know some basic features of Ruby and get an introduction with the different types of scopes and operators of Ruby. So, let’s continue ahead with other happening features of this graceful programming language.

Arithmetic Operators & Strings

So, in Ruby, you will observe the six arithmetic operators to try out; Addition (+), Subtraction (-), Multiplication (*), Division (/), Exponentiation (**), Modulo (%). The duty of string arithmetic is to complement the arithmetic operators in their operations. For example, if you put “I use” + “Ruby programming”. Then, your string will display the output as “I use Ruby programming”.

There are one another operators in Ruby present. Comparison operators which provide numerous comparison operators to check the equality between two values. These comparisons can be expressed as:

== Tests for equality

 

.eql? Same as ==

 

!= Tests for inequality

 

< Less than

 

>Greater than

 

>= Greater than/equal to

 

<= Less than/equal to

 

<=> Combined comparison operator

Ruby Class & Syntax

In Ruby, the class is like a blueprint that combines both the characteristics and functions with each other. From the class, individual objects can be formed. For example: in the case of the cuboid, it’s height, length and breadth can be denoted as the member of the class cuboid. Let’s Under the class better with this simple class ‘Test’.obj.

class Test

 

def welcome

 

puts “Welcome Fellow Coders to Ruby Programming!”

 

end

 

end

 

obj = Test.new

 

obj.welcome

Different Types Of Variable Scopes

Global Variable-The variables which can be easily added to the program anywhere is called as global variables. The global variable starts with the special character of the dollar sign ($). The global variable remains nil until the value of the variable is not declared by the user. The example of a global variable is like $radius, $counter, $name and $5. A global variable can be used as:

$glob

 

puts

 

Output:nil

 

$global = 10

 

class C

 

puts “in a class:

 

end

 

puts “at top-level, $global:

 

Output

 

  • in a class: 10

 

  • at top-level, $global: 10

Class Variable- The variable present within the single class structure is called as a class variable. Before using the class variables it is important to initialize them in the method definition. If the class Variable isn’t initialized, then the error will appear in the reference. Moreover, the class variable is shared between the instances of a class. The class variable starts with the @@. Like, @@total=0.

Instance Variable- This Variable helps in defining the characteristics or attributes of the object. To define the instance variable the special character @ is used. The instance variable cannot be accessed directly from the class. It can be only accessed within the object across the varied methods. Just like a global variable, until the value of the instance variable is not declared the variable remains nil. In creating the new object, this way instance variable can be used:

class Employee

 

def initialize(emp_id, emp_name)

 

@emp_id = emp_id

 

@emp_name = emp_name

 

end

 

def display

 

puts “Employee ID and Name: “

 

puts @emp_id, @emp_name

 

end

 

end

 

Employee.new(23434, “Anand”).display

 

Employee.new(343434, “Ram”).display

 

Output:

 

Employee ID and Name:

 

23434

 

Anand

 

Employee ID and Name:

 

343434

 

Ram

Local Variable- The local variable starts with either underscore or lower case alphabet. In local variable, scopes are defined in the end, module, def etc. The practical useful example of a local variable is:

name = “Anand”

 

def method1

 

name = “Anand”

 

puts “Name within method1: “, name

 

end

 

def method2

 

name = “Ram”

 

puts “Name within method2: “, name

 

end

 

method1

 

method2

 

method1

 

puts “Name outside methods: “+name

 

Output

 

Name inside method1

 

Anand

 

Name inside method2:

 

Ram

 

Name inside method1:

 

Anand

 

Name outside methods: Anand

Well, readers, we are not done yet, to know the other features of Ruby like hash, array, looping etc, don’t forget to check out the third episode of the series.

Send a Message