Object Oriented Ruby
Tags: ruby, programming, rails, class
Published: 2023-01-23 16:59:03

One of the core features of every programming language is Classes and Objects. Today I will teach you how to declare and use classes in Ruby.

Let’s get started!

Class names should start with an uppercase letter.
To declare a class, add a class keyword, then put a name, and don’t forget to add an end keyword.
Here is how it should look:

```
class Person

end
```
To make the process of learning easier you can think about classes like factories, that produce objects.

So now after we declared our class we should make it do something for us.

The first method you should declare is initialize method. What this method does is creates the instances of your object.

Here is an example:

```
class Person

  def initialize(first_name, last_name, age)
      @first_name = first_name    
      @last_name = last_name
      @age = age
  end

end

p1 = Person.new("Maxim", "Polyakov", 18)
binding.pry
```
Don’t freak out I will walk you through this code

Once we declared initialize method we added object instances as arguments, in our case it is: first_name, last_name, and age.
To understand the next part, let me explain how can we create a new object, the syntax is super simple, all you have to do is put the name of your class and add .new, then you can put first_name, last_name, and first name in parenthesis.
So, now what is happening inside our initialize method? We are creating instance variables with @ symbol and we assign our arguments to them.
Now after we create our first person we can use pry to see its instances in the terminal.

Now let’s say you want to change the value of an instance. To do that you have to create a setter method and give it the name of the instance you want to change.

```
class Person

  def initialize(first_name, last_name, age)
      @first_name = first_name    
      @last_name = last_name
      @age = age
  end
 
 # setter method
  def first_name=(first_name)
      @first_name = first_name
  end

end

p1 = Person.new("Maxim", "Polyakov", 18)
binding.pry

```
Here we are doing the same thing as in initialize, we are assigning our first_name argument to our object instance.

Cool so now we can change our instances, but what if we want to see them in our terminal? That is where the getter comes into a game. To declare a getter method you can just create a method with the same name as the instance you want to see.
```
class Person

  def initialize(first_name, last_name, age)
      @first_name = first_name    
      @last_name = last_name
      @age = age
  end
 
 # setter method
  def first_name=(first_name)
      @first_name = first_name
  end

  # getter method
   def last_name
      @last_name
   end

end
```
p1 = Person.new("Maxim", "Polyakov", 18)
binding.pry
Yup, getters are that simple, all you have to do is put an instance you want to see in the getter method.

The craziest part about is that you can make setter and getter in on a SINGLE line.

All you have to do is use attr_accessor.
```
class Person

  attr_accessor :age

  def initialize(first_name, last_name, age)
      @first_name = first_name    
      @last_name = last_name
      @age = age
  end
 
 # setter method
  def first_name=(first_name)
      @first_name = first_name
  end

  # getter method
   def last_name
      @last_name
   end

end

p1 = Person.new("Maxim", "Polyakov", 18)
binding.pry
```
Here all you have to do is pass in instances you want to update and see.

This is all you need to know to start using classes in your future ruby projects.

Happy Coding!