Ruby

Object Oriented Programming

Ruby Course

Introduction

You’ve got the building blocks of Ruby out of the way, great! Now it’s time to get into the fun stuff… how do we combine those building blocks in the most efficient and elegant ways to produce the programs we’d like to write?

The concepts you’ll learn here are often less specific to Ruby itself and more widely applicable to any object-oriented language. That’s because the fundamental concepts are just that… fundamental. Don’t repeat yourself. Modularize your code. Have your classes and methods only do one thing. Show as little of your interfaces to the world as you can. Don’t make methods or classes heavily dependent on each other. These will take some time and practice to implement effectively, but you’ll already be taking a big step towards creating high quality code just by finishing up this section.

There are two projects in this Object Oriented Programming Basics section, but this does not mean that you will only use OOP for these two projects. Ruby is a object-oriented language, so it is important to use OOP for all of the remaining projects.

There is a lot of reading in this lesson, so you are encouraged to code along with each example. If you find an example that does not make sense, ask for help in the #ruby-help channel in our Discord server. To get the most out of each example, try to apply what you learned by adding similar functionality on your own.

Lesson overview

This section contains a general overview of topics that you will learn in this lesson.

  • You will learn about classes.
  • You will learn about methods.
  • You will learn about scope.

Assignment

  1. Read the Object Oriented Programming with Ruby online book, by Launch School.
  2. Read through these reinforcing posts by Erik Trautman to help you answer the questions in the “Knowledge check” section:
  3. Read the article Object Relationships in Basic Ruby to see an example of how two classes can interact.
  4. Read the Bastard’s Chapter on Error Handling to reinforce your understanding of dealing with errors.
  5. Do this quiz on introductory object-oriented programming and this quiz on modules, classes, and inheritance from Code Quizzes.

Knowledge check

The following questions are an opportunity to reflect on key topics in this lesson. If you can’t answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.

Additional resources

This section contains helpful links to related content. It isn’t required, so consider it supplemental.

  • Kevin Berridge’s video presentation covers major themes of practical object-oriented design, with many references to Sandi Metz’s book, in about 40 minutes.
  • If you want more examples to code along with, check out Zetcode’s OOP section and Zetcode’s second OOP section until they start talking about exceptions (~80% of the way down).
  • 99 Bottles of OOP is a hands-on workbook that you should code along with to get the most out of it. This is a great resource if, after completing several OOP projects, you still feel uncertain about OOP concepts and would like to be guided through some refactoring examples and explanations.
  • Practical Object-Oriented Design in Ruby is a traditional technical book with practical examples. This is a great resource if, after completing several OOP projects, you still feel uncertain about OOP concepts and would like to be guided through some refactoring examples and explanations.

Support us!

The Odin Project is funded by the community. Join us in empowering learners around the globe by supporting The Odin Project!