DEV Community

0x-2FA
0x-2FA

Posted on

100 Days of Learning Ruby: Day #3

Originally when I was writing this blog I was going through what is Object Oriented programming (OOP) and then how we do it in ruby but the blog post became lengthy and I'm not here to teach programming. I'm just sharing my journey of learning Ruby in a tutorial style.

So instead I provided some links that can help you learn everything I was trying to teach you about OOP.

Day 3: Understanding Ruby a bit more

Now that we have the very very basics of ruby down it's time to learn more about it. Today we are going to learn the following:

  • Introduction to Object Oriented Programming (OOP)
  • OOP in Ruby (Part I)

Setup for Today

Create a new folder called Day_3, then create a new file with the name oop.rb and finally open it up in a text editor of your choice.

cd 100_days_of_ruby/
mkdir Day_3 && cd Day_3/ && touch oop.rb
codium .
Enter fullscreen mode Exit fullscreen mode

Introduction to Object Oriented Programming (OOP)

Everyone has heard of Object Oriented Programming and probably everyone has used it at least one in their career. So let's learn some things about it.

What is Object Oriented Programming (OOP)?

The wiki has a nice definition:

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

There is some fancy terminology here such as Programming Paradigm and we need to understand that first to be able to learn about OOP.

What is a Programming Paradigm?

Basically a programming paradigm is the concept by which the methodology of a programming language adheres to. Paradigms are important because they define a programming language and how it works.

There are a lot of paradigms out in the wild and in fact ruby is a multi paradigm language (aka supports more than one paradigm). We are not gonna discuss other programming paradigms today but here is an amazing site where you can learn more about them https://cs.lmu.edu/~ray/notes/paradigms/.

The Basic Idea of OOP

What are four basic principles of Object Oriented Programming?

What Is Object-Oriented Programming? The Four Basic Concepts of OOP

OOP in Ruby (Part I)

In most (if not all) ruby programs everything depends on objects. In fact most of the time in ruby you are creating new objects without even knowing it and that's because everything in ruby is an object. Also every object is obviously an instance of a class.

We can test that by adding the following lines of code to our program:

puts "Hello, World!".upcase # prints HELLO, WORLD!
puts "Hello, World!".class  # prints String
Enter fullscreen mode Exit fullscreen mode

Yes, we called a method on a plain string and that's because this "plain" string is in fact an object and it's an object of the class String.

Defining our own Classes in Ruby

This is very simple, just add the following to our program:

class Dog

end
Enter fullscreen mode Exit fullscreen mode

We just created our own class with the name Dog, it doesn't have any code yet but we will gradually add more things to it.

Defining our own Methods

Methods are super important to any Ruby class. Let's create our own by adding the following lines to our Dog class:

class Dog

  # return the age of the dog
  def GetAge()

  end

end
Enter fullscreen mode Exit fullscreen mode

As you can see we defined a new method called GetAge that at the moment doesn't do anything.

The way we define methods in ruby is pretty easy. Start with the def keyword followed by the name of your method, it can be anything you want, then a number of parameters and finally to close it we use the keyword end.

Keep in mind that we don't always need to have parameters to every method. In our case the method GetAge() doesn't have any parameters cause it simply doesn't need one.

Note: Generally you can name your methods however you want but don't name them initialize that's a special method that we will learn a bit later.

Also method names in ruby are generally written in snake_case so it would be better to write it like this get_age() but I'm used to methods being in PascalCase so that's why we wrote it GetAge().

Instance variables

These are just variables but defined inside of a class. Instance variables are available only to each instance of the class.

Add the following to our Dog class:

class Dog

  # return the age of the dog
  def GetAge()
    return @age
  end

end
Enter fullscreen mode Exit fullscreen mode

The @age is an instance variable, so instead of saying for example age = 4 we have to add the @ symbol @age = 4. That's the only difference in terms of syntax.

Also here we typed return @age, this is how we return a value from a method and it's the same with a lot of other programming languages. In ruby though we can omit the keyword return and just type @age. That's because in ruby every method returns the result of the last expression, in our case it will just return the value of @age.

But I like to always know what a method returns as I go through someone's code (even mine), so I like to explicitly return a value from a method.

Lastly add the following method to our Dog class:

class Dog

  # return the age of the dog
  def GetAge()
    return @age
  end

  # set the age of the dog
  def SetAge(age)
    @age = age
  end
end
Enter fullscreen mode Exit fullscreen mode

Defining an object of class Dog

Add the following lines of code after the Dog class:

my_dog = Dog.new()   # create a new object of class Dog

puts my_dog.class    # print the class of the my_dog object

puts my_dog.GetAge() # print the age of the dog before setting it
my_dog.SetAge(4)
puts my_dog.GetAge() # print the age of the dog after setting it
Enter fullscreen mode Exit fullscreen mode

Special initialize method

The initialize method is a special type method that we can define in every class. What it does it's pretty simple, if we have it defined it will execute as soon as we create a new instance (object) of the class. To test it let's define it in our Dog class:

class Dog

  def initialize()
    @age = 3
  end

  # return the age of the dog
  def GetAge()
    return @age
  end

  # set the age of the dog
  def SetAge(age)
    @age = age
  end

end
Enter fullscreen mode Exit fullscreen mode

Now run our program again and pay attention that the first puts my_dog.GetAge() instead of printing nothing, it now prints the value 3 cause when we did my_dog = Dog.new() we basically created a new object and it immediately called the initialize method.

We will end Day #3 here but we haven't learnt everything about OOP in Ruby that's because this blog became a bit lengthy so I decided to break it into two parts. So next time we will continue with OOP in Ruby and learn more things such as inheritance.

Thanks for joining, I will see you next time!

Top comments (0)