Chevron Left Icon
Object-Oriented Programming I
Lesson 1 of 2
Chevron Right Icon
  1. 1

    Ruby is an object-oriented programming language, which means it manipulates programming constructs called objects. (Almost) everything in Ruby is an object! You’ve been using them all along, so t…

  2. 2

    A basic class consists only of the […] keyword and the name of the class. Check it out: […] Our […] has the ability to create new Ruby objects of class […] (just like […] is a …

  3. 3

    We’d like our classes to do more than… well, nothing, so we’ll have to add some code between our […] and […] . You may have noticed in our example back in the first exercise that we start…

  4. 4

    All right! Just one more step before we can create a person from our […] class: we have to make sure each person has a […] . In Ruby, we use […] before a variable to signify that it’s a…

  5. 5

    Perfect! Now we’re ready to start creating objects. We can create an instance of a class just by calling […] on the class name, like so: […]

  6. 6

    Another important aspect of Ruby classes is scope. The scope of a variable is the context in which it’s visible to the program. It may surprise you to learn that not all variables are accessible…

  7. 7

    Recall that instance variables begin with an […] . This isn’t just a Ruby convention—it’s part of the syntax! Always start your instance variables with […] . Class variables are like insta…

  8. 8

    Good! A caveat, though: global variables can be changed from anywhere in your program, and they are generally not a very good idea. It’s much better to create variables with limited scope that can …

  9. 9

    We can create class variables by starting a variable name with two […] symbols. Class variables are attached to entire classes, not just instances of classes, like so: […] Because there’s o…

  10. 10

    Classes like […] and […] are great when you are starting to learn the concepts of classes and instances. However, classes and objects are often used to model real-world objects. The code i…

  11. 11

    Inheritance is a tricky concept, so let’s go through it step by step. Inheritance is the process by which one class takes on the attributes and methods of another, and it’s used to express an *i…

  12. 12

    In Ruby, inheritance works like this: […] The derived class is the new class you’re making and the base class is the class from which that new class inherits. You can read “ […] “ as “inherit…

  13. 13

    Sometimes you’ll want one class that inherits from another to not only take on the methods and attributes of its parent, but to override one or more of them. For instance, you might have an […]…

  14. 14

    On the flip side, sometimes you’ll be working with a derived class (or subclass) and realize that you’ve overwritten a method or attribute defined in that class’ base class (also called a *parent…

  15. 15

    Any given Ruby class can have only one superclass. Some languages allow a class to have more than one parent, which is a model called multiple inheritance. This can get really ugly really fast,…

  16. 16

    All right! Let’s do a little review.

  17. 17

    Perfect! Now let’s class things up a bit with a class variable.

  18. 18

    Perfect! Let’s go ahead and create an instance of our […] class.

  19. 19

    Perfect! Now let’s get in a little practice with inheritance.

  20. 20

    You’re a champion! Our last topic: Ruby’s […] keyword. (We’ve decided we liked […] ‘s […] method after all.)

  1. 1

    Now that you’ve learned all about classes and objects in Ruby, you can create any kind of Ruby object your heart desires. In this project, we’ll use our newfound knowledge to create a class, […]…

  2. 2

    All right! Let’s go ahead and set up our class.

  3. 3

    Well done! Now let’s spice up our […] method by passing it some parameters and setting some instance variables.

  4. 4

    Let’s go ahead and toss in a class variable. We’ll use the variable […] to keep track of all the users on our computer.

  5. 5

    Excellent! Our class is coming together, but it’s a bit boring. Let’s add another method, […] . We’ll want to do a few things in it:

  6. 6

    Perfect! Now that we think of it, though, we could have a whole bunch of users creating files every which where, and we don’t have a way of getting to our […] class variable! We’ll need to crea…

  7. 7

    Excellent! Last step: let’s create an instance of our […] class. You’ve done this before, but here’s a refresher. […] 1. In the example above, we first define a […] class with an [….

  8. 8

    Great work! You’ve got your very own […] class that generates virtual computer instances. Feel free to play around with your class. What if you add a method that updates files? Or deletes them…