Learn How to Code
  • Welcome
  • Foundations
    • Introduction
      • Becoming a web developer
      • Motivation and mindset
      • Join a supportive community
      • How does the web work?
    • Requirements
      • Prerequisites
      • Text editors
      • Command line basics
      • Setting up Git
      • Setting up Node
    • Git
      • Git basics
      • Project: Practicing Git
    • Frontend
      • HTML and CSS
      • Developer Tools
      • Project: Create a web page
    • JavaScript
      • Strings and Conditionals
      • Using Developer Tools
      • Functions
      • Problem solving
      • Project: Rock paper scissors
      • Writing clean code
      • Arrays and Loops
      • The DOM
      • Project: Etch-A-Sketch
      • Objects and More Arrays
      • Project: Calculator
    • Backend
      • Frameworks
    • Next steps
  • Deep dives
    • Computer Science
      • Pseudocode and algorithms
      • Recursion and algorithms
        • Project: Fibs and sorting
        • More on algorithms
        • Big O
        • Project: Practicing Big O
      • Data structures
        • Maps, Stacks and Queues
        • Project: Stacks and Queues
        • Nodes, Linked Lists and Trees
        • Project: Linked Lists
        • Project: Trees
        • Next steps
    • Databases
      • Databases and SQL
      • Project: SQL Zoo
    • Design / UX
      • Fonts and typography
      • Grids
      • Project: Teardown
      • Responsive design
      • Project: Mobile friendly
      • CSS frameworks
      • Project: Bootstrapping
    • HTML / CSS
      • HTML Basics
        • Linking
        • Images and media
        • Project: Embedding media
        • HTML5
        • Tables
        • Lists
        • Forms
        • Project: Make a form
      • CSS Basics
        • Box model
        • Floats and positioning
        • Flexbox
        • Grids
        • Project: Breaking news
        • Best practices
        • Backgrounds and gradients
        • Project: One more thing
        • CSS3
        • Preprocessors
        • Project: Your own framework
      • Next steps
    • JavaScript
      • Refresher
      • Organization
      • Objects and constructors
      • Project: Library
      • Factory functions and module patterns
      • Project: Tic Tac Toe
      • Classes
      • ES6 Modules
      • Project: Restaurant
      • Object Oriented Programming
      • Project: Todo list
      • Linting
      • Menus and sliders
      • Forms
      • ES6 features
      • JSON
      • Callbacks and promises
      • Using APIs
      • Async and Await
      • Project: Weather
      • Testing
      • Project: Testing 1-2-3
      • Advanced Testing
      • Project: Battleship
      • Backends
      • Project: Where's Waldo?
      • Project: All-Star
      • Next steps
    • NodeJS
      • Project: Going to school
      • Project: Passing the test
      • Express
        • Templates and middleware
        • CRUD and MVC
        • Project: Message board
        • Routes
        • Displaying data
        • Forms and deployment
        • Project: Inventory
      • Authentication
      • Security
      • Project: Clubhouse
      • APIs
      • Securing an API
      • Project: Blog
      • Testing
      • Testing with a database
      • Project: Social network
    • React
      • Props and State
      • Render lists and handle inputs
      • Project: CV
      • Lifecycle methods
      • Hooks
      • Project: Memory card
      • Router
      • Project: Shopping cart
      • Advanced concepts
    • Ruby
      • Installation
      • Data types
      • Variables
      • Input and Output
      • Conditionals
      • Loops
      • Arrays
      • Hashes
      • Methods
      • Enumerables
      • More enumerables
      • Nested collections
      • Blocks
      • Pattern matching
      • Debugging
      • Project: Caesar cipher
      • Project: Substrings
      • Project: Stock picker
      • Project: Bubble sort
      • Object oriented programming
      • Project: Tic Tac Toe
      • Project: Mastermind
      • Serialization
      • Project: Event manager
      • Project: Hangman
      • Computer Science
        • Recursion
        • Project: Merge Sort
        • Data structures and algorithms
        • Project: Linked Lists
        • Project: Binary Search Trees
        • Project: Knight Travails
      • Testing
      • RSpec
      • Project: Four in a row
      • Git
      • Project: Open Source
      • Project: Chess
      • Next steps
    • Ruby on Rails
      • Using Heroku
      • Installing Rails
      • Basics
        • Routing
        • Controllers
        • Views
        • Asset pipeline
        • Deployment
        • Project: Blog
      • Active Record
        • Project: Upvote
      • Forms
        • Cookies, sessions, and authentication
        • Project: Password
      • Advanced forms and Active Record
        • Associations
        • Project: Private Events
        • Callbacks
        • Menus, helpers and nested forms
        • Project: Ticket agent
      • APIs
        • External APIs
        • Project: Animals
        • Project: Photo widget
      • Mailers
        • Project: Confirmation
      • Advanced topics
        • Action Cable
      • Project: Social network
      • Next steps
  • Getting hired
    • Preparing to find a job
      • Plan a strategy
      • What companies want
      • Get yourself together
      • How to prepare
      • Project: Make your website
    • Applying and interviewing
      • Qualifying leads
      • Project: Make your resume
      • Applying for jobs
      • Preparing for an interview
      • Handling an offer
      • Final words
  • Maintained by
    • wbnns
  • License
    • CC BY-NC-SA 4.0 © 2022
Powered by GitBook
On this page
  • Introduction
  • Learning outcomes
  • Classes and methods
  • Scope
  • Assignment
  • Extra credit
  • Additional resources
  1. Deep dives
  2. Ruby

Object oriented programming

Learn more about 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

Look through these now and then use them to test yourself after doing the assignment:

Classes and methods

  • What is an implicit return?

  • What is a class?

  • When should you use a class?

  • How do you create a class in your script file?

  • What is an instance of a class?

  • What is the difference between the Pascal Case and snake_case styles of naming?

  • How do you instantiate a class?

  • How do you set the state of your new instance?

  • What should be done in the #initialize method?

  • What is a class method?

  • How is a class method different from an instance method?

  • How are methods you already know like #count or #sort etc instance methods?

  • What's the difference between how you declare a class method vs an instance method?

  • What's the difference between how you call a class method vs an instance method?

  • What is an instance variable?

  • What's the difference between an instance variable and a 'regular' variable?

  • What are "getter" and "setter" methods used for?

  • What is the difference between a "getter" and a "setter" method?

  • How do you make instance variables readable outside your class? Writeable? Both at the same time?

  • Can a class call its own class methods?

  • What's the difference between when you would use a class variable and when you would use a constant?

  • What's the difference between a class and a module?

  • When would you use a class but not a module?

  • How does inheritance work?

  • Why is inheritance really useful?

  • How do you extend a class? What does that mean?

  • What does #super do? Why use it?

Scope

  • What is scope?

  • When can you start using a variable?

  • When is a new scope defined?

  • When are methods in scope?

  • What is a private method?

  • What is a protected method?

  • How are private and protected methods different?

  • What does "encapsulation" mean?

Assignment

    • Object-Oriented Programming, Part I

    • Project: Virtual Computer

    • Object-Oriented Programming, Part II

    • Project: Banking on Ruby

  1. Take a brief break from code and learn more about the world of Ruby:

  2. Read through these reinforcing posts by Erik Trautman to help you answer the questions in the "Learning Outcomes" section:

    • Your code editor may have extensions that will utilize rubocop. For example, VSCode has two extensions: 'VSCode Ruby' and 'Ruby'. The 'Ruby' extension allows a rubocop dropdown option for a setting called, Ruby: Format.

    • 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.

    • Please note: If you followed the above installations and the rubocop linter of your Ruby extension is still not highlighting offenses within VSCode, you may alternately try the ruby-rubocop extension. This extension lists potential problems if using a rvm or chruby environment and should only be used if you installed Ruby with rbenv as instructed in the Installing Ruby lesson. The ruby-rubocop extension also states that when autoCorrect is enabled, the history of changing file is broken.

Extra credit

  1. Make sure you go back up and look at all the questions from the "Learning Outcomes" section. See if you can do most of them without looking back through the text.

Additional resources

This section contains helpful links to other content. It isn't required, so consider it supplemental for if you need to dive deeper into something.

PreviousProject: Bubble sortNextProject: Tic Tac Toe

Last updated 4 years ago

Do :

Read about the

Read about in Section 1

Read about where you can find

Read through for a more thorough understanding.

Read the article to see an example of how two classes can interact.

Read the to reinforce your understanding of dealing with errors.

Glance over the so you have an idea of how to make your code look more professional. It is recommended to install , a static code analyzer (linter) and formatter, which is based on this style guide.

Read the of rubocop in the terminal.

Make sure you can do from .

Make sure you can do as well.

, by Sandi Metz, described as "a practical guide to writing cost-effective, maintainable, and pleasing object-oriented code." Available in Ruby and JavaScript (bundled together).

If you'd still like to learn more about how to really design more concise, effective, purposeful classes, check out , also by Sandi Metz.

covers major themes of practical object-oriented design, with many references to Sandi Metz's book, in about 40 minutes.

.

Now you're ready to read through .

Read through until they start talking about exceptions (~80% of the way down).

Codecademy Ruby sections 9 and 10
History of Ruby
Open Source Culture
Ruby's Community
Launch School's OOP book
Ruby Explained: Classes
Ruby Explained: Inheritance and Scope
Object Relationships in Basic Ruby
Bastard's Chapter on Error Handling
Ruby Style Guide
rubocop
basic usage
Quiz #5
Code Quizzes
Quiz #7
99 Bottles of OOP
Practical Object-Oriented Design in Ruby
This video presentation from Kevin Berridge
Zetcode's Variables section
Zetcode's OOP section
Zetcode's second OOP section