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 class keyword and the name of the class. Check it out: class NewClass # Class magic here end Our NewClass has the ability to create new Ruby object…
  3. 3
    We’d like our classes to do more than… well, nothing, so we’ll have to add some code between our class Person and end. You may have noticed in our example back in the first exercise that we star…
  4. 4
    All right! Just one more step before we can create a person from our Person class: we have to make sure each person has a @name. In Ruby, we use @ before a variable to signify that it’s an *instan…
  5. 5
    Perfect! Now we’re ready to start creating objects. We can create an instance of a class just by calling .new on the class name, like so: me = Person.new(“Eric”)
  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 instance variable…
  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: class MyClass @@class_variabl…
  10. 10
    Classes like Language and Person 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: class DerivedClass < BaseClass # Some stuff! end The derived class is the new class you’re making and the base class is the class from which that new class …
  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 Email …
  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 Message 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 super keyword. (We’ve decided we liked Message’s initialize 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, Machin…
  2. 2
    All right! Let’s go ahead and set up our class.
  3. 3
    Well done! Now let’s spice up our initialize 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 @@users 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, create. 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 @@users class variable! We’ll need to crea…
  7. 7
    Excellent! Last step: let’s create an instance of our Computer class. You’ve done this before, but here’s a refresher. class Person def initialize(name) @name = name end end emma = Perso…
  8. 8
    Great work! You’ve got your very own Computer 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 the…