Object-Oriented Programming, Part I
Learn how to organize information and behavior in a program with Ruby’s object-oriented concepts such as classes, objects, and inheritance.
StartKey Concepts
Review core concepts you need to learn to master this subject
Ruby Class Variables
Ruby .new Method
Ruby Instance Variable
Ruby initialize Method
Ruby Class
Ruby super Keyword
Ruby attr_reader attr_writer Methods
Ruby Class Variables
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 = Child.new("Naomi", 2006)
bertha = Child.new("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
- 1Ruby 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…
- 2A 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…
- 3We’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…
- 4All 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…
- 5Perfect! 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”)
- 6Another 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…
- 7Recall 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…
- 8Good! 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 …
- 9We 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…
- 10Classes 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…
- 11Inheritance 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…
- 12In 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 …
- 14On 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…
- 15Any 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,…
- 16All right! Let’s do a little review.
- 17Perfect! Now let’s class things up a bit with a class variable.
- 18Perfect! Let’s go ahead and create an instance of our Message class.
- 19Perfect! Now let’s get in a little practice with inheritance.
- 20You’re a champion! Our last topic: Ruby’s super keyword. (We’ve decided we liked Message’s initialize method after all.)