Introduction to How OOP Works in Ruby
In this lesson, we are going into look into a fundamental concept called Object Oriented programming. If you're coming from other object-oriented programming languages, you may be familiar with some of these concepts, but still it's important you pay attention because Ruby embraces a very specific form of object-oriented programming compared with many other languages available today.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson we are going into look into a fundamental concept called Object Oriented programming. If you're coming from other object-oriented programming languages, you may be familiar with some of these concepts, but still it's important you pay attention because Ruby embraces a very specific form of object-oriented programming compared with many other languages available today.

Before we begin, a key thing to remember is that pretty much everything is an object in Ruby. We'll see more of it as we go. Also, I'm going to show some practical ways of using object-oriented programming which hopefully be more enlightening than the theoretical approach taught in most computer science books. In fact, teaching a practical application of object-oriented programming was one of my biggest motivators to create this course.

When I was studying development in college I thought I learned object oriented programming. However what I learned in school did little to help me build real world programs and I ended up having to re-learn a practical approach to OOP as I taught myself how to code.

Real World OOP Example

Let's start by analyzing a Ruby on Rails project that I built for a client.

large

If you see the first line, I have a class called ApplicationController which inherits from a class called ActionController::Base. The < symbol denotes inheritance in Ruby.

devCamp Notes: make sure not to get the < used for inheritance with the < symbol used with conditionals. The Ruby parser is smart enough to differentiate when you want to use one over the other.

What this code essentially means is that, ApplicationController has access to all the methods available in ActionController::Base. In this sense, the attributes and behavior of a class is determined largely by the class from which it inherits.

Now, there can be classes that inherit from ApplicationController. I'm going to open another file to show you how this works.

large

The first line tells you that the BranchesController is a class that inherits from the ApplicationController. Due to this inheritance, methods present in ApplicationController such as index and new can be accessed by BranchesController. The same applies to before_action method too. I've not declared this method anywhere in BranchesController, yet I have access to it because it is present in ApplicationController. But wait! There was no mention of these messages inside of the ApplicationController, so what gives? Don't worry, it's not coding black magic. These methods are passed down from the ActionController::Base from which the ActionController inherits from.

If this is still fuzzy let's think of a real world example of inheritance. As humans, each of us have biological parents. Our parents passed down a set of genes to us that our bodies have access to. In this analogy our parents are like the ActionController::Base class and we are the BranchesController. The genes that our parents pass to us are like the methods that our BranchesController has access to.

Let's take a look at another code example from the Rails project, let's go to the create method in the BranchController.

large

If you see, in line 27, I'm creating a new instance of the class called Branch and passing some custom parameters to it. I can access the methods of this Branch class through the branch instance I created here.

Now, if I go to the Branch class, you can see that it inherited from another class called ActiveRecord::Base.

large

By inheriting from the ActiveRecord::Base class our Branch will have access to a large number of methods that it can call when it needs them. The methods that our Branch class inherits allows it to:

  • Communicate with the database
  • Implement data validations
  • Launch automated processes named callbacks
  • And much more

When you get into Ruby on Rails application development you'll see inheritance is used extensively.

If you've never worked with OOP before this may all seem a bit overwhelming, however don't let it scare you off. I wanted to start by showing you a practical example of how object oriented programming works because I was tired of instructors who gave contrived OOP examples. I'd prefer for you to see what object oriented programming does in a real application so you can realize how important it is.

In the next few guides we will building out our own application which will be an API connector that can communicate dynamically with application on the web.