Ruby (part-10)

Ruby (part-10)

Introduction

Till now we only learned about simple concepts that are very easy to understand Hold on !! I make you understand easily through my blogs, Just kidding never mind :). Openly say whenever I reach this concept I feel like entering a new world of known things but things make no sense while working. As I have basic knowledge of Oops with python through ruby Oops I get clarity even more. In simple words, through Oops, we can get real-time software-building experience.

Why

This is the concept that I am super curious about. I DO write a blog separately on this. Simply put, this is of the programming paradigms used in most software design.

Class

The basic definition of class is "It is the basic building block of Object Oriented Programming(OOP)&they help you define a blueprint for creating objects."

Let's understand the definition it would be great if we break it.

Before that DO try to revise simple ruby code. At starting we discussed that like a python in ruby, everything is an object.

a = 10

Here "a" is an object. The conversation of data type is explicitly done in ruby. So it is happening internally we can't even bother about it right!!

Does the question raise?

Why we are not getting any error while storing 10 in the "a"

This is because of the blueprint. Interpreter has an inbuilt ruby integer class that explicitly assigns the value accordingly.

Shall we create our own objects as per our wish the answer is "yes" we can. This is also one of the reasons why Oops came into existence.

We can create any blueprint as we wish and create any objects for that.

Terminology

Does the class have different terminology?

The answer is NO. but it's not terminology we can call it different names

in the classes

variables are called--> attributes

functions/methods are called--> behaviors

Syntax

The class can be created by using the class keyword followed by the class name. By convention, the class name starts with a capital letter. Like loops and conditional statements, classes also have to finish with the "end" keyword.

class Person
end

Instances

An instance of the class is called an object.

first, look at what is an instance.

an instance can be created by class_name followed by a new keyword.

obj  = Person.new

The new literally told the interpreter to create separate memory for this class.

Adding arguments after new is depends we can look at this while learning constructor.

Let's try to create our own object for our blueprint

class Person
    print "malavi pande"
end
obj = Person.new
output:
malavi pande

Compare

This is not at all related to the current topic but this is the most underrated skill we have to build in my opinion.

let's understand this with the code base

name = "Malavi"
print name.length
output:
6

In the above code name is the string class instance and length is the method in that class. So, in this way, everything is an object in the ruby.

Scoping

Everything present in the code editor is not accessible everywhere. Technically we can call it scoping. We can even specify using variables.

There are mainly 3 types of variables are there

  1. Global variables

  2. Instance variables

  3. classes variables

Global variables

The name itself indicates that it is available everywhere. We can say it is a global variable by placing "$" in front of that variable.

class Person
    $name = "Malavi"
    def name
        puts $name
    end
end
obj = Person.new
obj.name
output:
Malavi

Instance variables

Before getting into this variables scoping we have to first know about the initialize method. This is the same as python into methods. In even more simple terms we can call it a constructor. This is the special method in the class.

In general, we call the class methods using objects. But this construct method executes while creating an object, it is explicitly classed by the class only.

The variables defined inside the constructor are class instance variables.

class Person
    def initialize(name,surname)
        @name = name #instance variables
        @surname = surname
    end
    def display
        puts @name + @surname
    end
end
obj = Person.new("malavi", "pande")
obj.display
output:
malavipande

This is the magic that happens with the instance variables

Class Variables

Class variables are like instance variables but instead of belonging to an instance of a class, they belong, the class, itself. They are mainly useful to keep track.

class Person
    @@count = 0
    def display
        @@count += 1
    end
end
obj = Person.new
puts obj.display
output:
1

Public&Private methods

This concept came into the picture because of security purposes. So, the methods start with private keywords that can only be accessible inside the class only. If you leave any method inside the class explicitly it can be considered a public method.

class Person 
    public
    def email
        print "malavipande7@gmail.com"
        password
    end
    private
    def password
        print "123456"
    end
end
obj = Person.new
obj.email # this print the email
obj.password # this alters us with warning redarding private keyword

If you define any method after declare the previous method with private keyword by default after that method ruby cosiders everything as private.

So, the best practice is define all the public methods before private methods start.

Conclustion

This is all about Oops introduction. This is my way of understanding and explanation of object oriented programming. I am pretty sure that it is not possible to understand the this concept in one go. But do try to relate things while learning it help to get things more clear. Feel very free to comment below about the blog.

Did you find this article valuable?

Support Malavi Pande by becoming a sponsor. Any amount is appreciated!