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 “Learning Outcomes” 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 Quiz #5 and Quiz #7 from Code Quizzes
  6. Every programming language community develops a style guide to help make code more maintainable and easier to read, therefore it is important to familiarize yourself with the Ruby Style Guide.
    • As you can see, there are a lot of guidelines. Instead of trying to remember everything, install rubocop, a static code analyzer (linter) and formatter, which is based on this style guide.
    • When it suggests a change that you don’t understand, you can refer to this style guide to understand the reasons behind the rule. You will be inundated with offenses that seem minor, but we encourage you to make the recommended adjustments and trust the wisdom of the Ruby community that developed this style guide. If you feel strongly that you should ignore a particular rule, you can research ways to disable a particular rule or even ignore an entire file.
    • Read the basic usage of rubocop in the terminal.
    • To highlight the rubocop offenses in VSCode, you will need to have the ‘Ruby’ extension installed. In addition, you will need to update your settings.json file with the following lines:
"ruby.lint": {
  "rubocop": true,
},

If the above instructions do not work, explore the initial configuration options in the extension’s documentation. Another alternative is to try the ruby-rubocop extension, but be aware of the potential problems listed in their documentation.

Knowledge check

This section contains questions for you to check your understanding of this lesson. If you’re having trouble answering the questions below on your own, review the material above to find the answer.

Additional resources

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

  • This video presentation from Kevin Berridge covers major themes of practical object-oriented design, with many references to Sandi Metz’s book, in about 40 minutes.
  • If you need a variable refresher, check out Zetcode’s Variables section.
  • 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).
  • After you have completed several OOP projects, the following books by Sandi Metz are essential to build on the foundational concepts in this lesson.
    • 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 you feel uncertain about OOP concepts and would like to guided through refactoring examples and explanations.
    • Practical Object-Oriented Design in Ruby is a traditional technical book with practical examples. This is a great resource after 99 Bottles or if you already feel like you fully understand OOP concepts.

Support us!

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