Key Concepts

Review core concepts you need to learn to master this subject

Ruby Class Variables

class Child @@children = 0 def initialize(name, birth_year) @name = name @birth_year = birth_year @@children +=1 end def self.children_added return @@children end end naomi ="Naomi", 2006) bertha ="Bertha", 2008) puts Child.children_added # => 2

In Ruby, class variables are attached to the class in which they are declared. A class variable should be declared with two @ symbols preceding it.

Object-Oriented Programming I
Lesson 1 of 2
  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 =“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.)