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 super Keyword
Ruby Class
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