Object Oriented Programming

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. Be lazy. 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’s a lot to do here but stick with it! We’ll start with the Codecademy lessons, which are interspersed with their projects so you’ll get a chance to apply what you’re learning. The Launch School’s OOP book will help you understand the material a bit deeper, which will be important when you start creating your own projects.

Learning Outcomes

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

Assignment

  1. Do Codecademy Ruby sections 9 and 10:
    1. Object-Oriented Programming, Part I
    2. Project: Virtual Computer
    3. Object-Oriented Programming, Part II
    4. Project: Banking on Ruby
  2. Take a brief break from code and learn more about the world of Ruby:
    1. Read about the History of Ruby
    2. Read about Open Source Culture in Section 1
    3. Read about where you can find Ruby’s Community
  3. Read through Launch School’s OOP book for a more thorough understanding.
  4. Read through these reinforcing posts by Erik Trautman to help you answer the questions in the “Learning Outcomes” section:
    1. Ruby Explained: Classes
    2. Ruby Explained: Inheritance and Scope
  5. Read the article Object Relationships in Basic Ruby to see an example of how two classes can interact.
  6. Read the Bastard’s Chapter on Error Handling to reinforce your understanding of dealing with errors.
  7. Do Quiz #5 from Code Quizzes.
  8. Do Quiz #7 as well.
  9. Glance over the Ruby Style Guide so you have an idea of how to make your code look more professional. It is recommended to install rubocop, a static code analyzer (linter) and formatter, which is based on this style guide.
    1. Read the basic usage of rubocop in the terminal.
    2. 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.

As you begin to use rubocop, you will be inundated with multiple offenses that seem minor. At this point in your Ruby knowledge, make the recommended adjustments and trust the wisdom of the Ruby community that developed this style guide. Research the offenses that you do not understand. 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.

Additional Resources

This section contains helpful links to other 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.
  • Zetcode’s Variables section.
  • Now you’re ready to read through Zetcode’s OOP section.
  • Read through Zetcode’s second OOP section until they start talking about exceptions (~80% of the way down).
  • Both of the below books are paid and optional, and not everyone agrees on exactly when you should read them. However, it is agreed that the resources are extremely valuable, therefore some guidelines have been set as to when you should attempt to (optionally) read them:
    • Once you have gotten a few OOP projects completed (Tic-Tac-Toe, Mastermind, Hangman, and Custom Enumerables at minimum) you should be ready to read 99 Bottles of OOP, by Sandi Metz, to start learning how to refactor your code in a more OOP way.
    • Once you feel comfortable after learning to refactor existing code with 99 Bottles, the next logical step would be learning Practical Object-Oriented Design in Ruby, also by Sandi Metz.

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.

Improve this lesson on GitHub

Have a question?

Chat with our friendly Odin community in our Discord chatrooms!

Open Discord