Object Oriented Programming
Tags: ruby, webdev, programming
Published: 2022-08-02 07:04:00

As a beginner programmer, it is important to understand what object-oriented programming is and how it can make your program better. `Object-oriented programming` is a paradigm that centers around using real life by using objects, as the name suggests, and basing the code around these objects. There are languages like Java or Ruby that are OOP languages and are created to be used as such. To better understand object-oriented programming and how to utilize it and the important qualities that come along with it, we will be using Ruby in the examples.

#The Basics of OOP
It may seem scary to tackle a new concept that seems so daunting, but it is actually quite simple once it is broken down and with practice. In fact if you have already been using Ruby, you have most likely been using objects and have not known. String, Integer and Array are all examples of different types of objects that are pre-defined for us by Ruby.
```ruby
str = "This is an object"
```
The above example is how you may be used to creating a new string, but you can also do it by writing the following:
```ruby
str = String.new("This is an object")
```
This may look a bit different but it is doing the same thing. The first example is just using a bit of `syntactic sugar` to make it cleaner and more visually appealing since Strings are a very commonly used object, Ruby helps us out a bit. What we are doing in this example is creating a `new` instance of the String class and assigning the value to be what is inside the parentheses. You may be wondering what a class is and that is what we will discuss in the next section.

#Classes
A `class` is how you are able to create your own objects. They are essentially a template that will define the properties and methods of an object. Imagine you are building a library, you would need a lot of books. You could create each book individually, and add all the necessary methods and properties. However, all books are essentially the same minus the details. They have a title, authors, number of pages, you can read them, bookmark them, etc. It would be much easier to create a book class, and you create as many book objects as you need to fill your library, with overly repetitive code. Let's start with how to declare a class. 
```ruby
class Book
#code describing class here
end
```
It is quite straightforward, you simply use the class keyword following the name of you class with the first letter capitalized. If your class name is a combination of multiple words use camel cases to separate the other words. For example, if your class is describing dog toy objects the class name would be `DogToy`. To signal the end of the class use the `end` keyword. Between those two elements is where all your code will go. In order to create a book object you would do the following:
```ruby
mistborn = Book.new
```
There is that new word again. Whenever you are creating an instance of a class you use the class name followed by the new keyword, and that will create a new instance.

Important elements to have in a class are getter and setter methods for the instance variables. The `setter` method allows us to set or change the variable, whereas the `getter` method allows us to retrieve the value of the instance variable. You can type these out or you can use ruby macros that do the heavy lifting for you. They are the `attr_writer`, `attr_reader` and `attr_accessor` macros. `attr_writer` will define a setter method for a variable, `attr_reader` will define a getter method, `attr_accessor` is an even shorter short cut for when you need a getter and setter method for a particular variable. The code snippet below is an example of how you would use these. Each instance variable is separated by a comma.
```ruby
attr_accessor :title, :author, :num_of_pages, :current_page
```
The next important item to discuss about classes is the initialize method. The initialize method is not called by its name like you are used to, but instead it is called when you create an instance using the new keyword. You can even use parameters to initialize your instance variables within the method.
```ruby
def initialize(title, author, num_of_pages)
   @title = title
   @author = author
   @num_of_pages = num_of_pages
   @current_page = 0
end
```
The @ refers to the instance variable and allows you to set it with the values passed in. If you wanted to create a new book object and pass in those values you would simply add those values after the new keyword.
```ruby
mistborn = Book.new("Mistborn", "Brandon Sanderson", 792)
```
Now let's look at an example of all of those elements put together along with some instance methods.
```ruby
class Book
   attr_accessor :title, :author, :num_of_pages, :current_page

   def initialize(title, author, num_of_pages)
      @title = title
      @author = author
      @num_of_pages = num_of_pages
      @current_page = 0
   end

   def read
      puts "I am reading #{self.title}"
   end

   def bookmark(page)
      self.current_page = page
   end

   def finish
      puts "I am finished reading #{self.title}"
   end
end
```
We have a fully functional Book class. You should now have an understanding of what almost everything above means and does. The only thing you may not understand is the `self` keyword. The self keyword allows you to refer to that particular instance.
```ruby
mistborn = Book.new("Mistborn", "Brandon Sanderson", 792)
mistborn.read
#will output: I am reading Mistborn
```
The self keyword allows you to access the value within the instance variable in the class. So you can do things like shown above.

#Conclusion
You should now be able to create basic classes and have a better understanding of what object-oriented programming is and how it can help your code be less repetitive and more modular. There is still a lot more to OOP, like inheritance and polymorphism, but you are now equipped to tackle those topics.

##Resources 
* [Object-oriented programming](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming) 
* [Object-Oriented Programming]
(https://www.rubyguides.com/ruby-tutorial/object-oriented-programming/)