Classes


Collapse Content

Class Code

# Define a class with the class keyword
class Human

  # A class variable is shared by all instances of a class. 
  # (Note: Using it is discouraged)
  @@species = "H. sapiens"

  # Basic initializer (a.k.a. 'constructor')
  def initialize(name, age=18)
    # Assign the argument to the "name" instance variable for the instance
    @name = name
    # If no age passed in, will be given default value of 18 
    @age = age
  end

  # Basic setter method
  def name=(name)
    @name = name
  end

  # Basic getter method
  def name
    @name
  end

  # Ruby has a one-line shortcut for creating a getter and setter:
  attr_accessor :name

  # Getter/setter methods can also be created individually like this
  attr_reader :name
  attr_writer :name

  # A class method uses self to distinguish from instance methods.
  # It can only be called on the class, not an instance.
  def self.say(msg)
    puts "#{msg}"
  end

  def species
    @@species
  end

end

Using Classes and Objects

# Instantiate a class
jim = Human.new("Jim Halpert")    
dwight = Human.new("Dwight K. Schrute")

# Let's call a couple of methods
jim.species #=> "H. sapiens"
jim.name #=> "Jim Halpert"  #calls getters
jim.name = "Jim Halpert II"  #calls setter
#=> "Jim Halpert II"
jim.name #=> "Jim Halpert II"
dwight.species #=> "H. sapiens"
dwight.name #=> "Dwight K. Schrute"

# Call the class method
Human.say("Hi") #=> "Hi"

# Variable's scopes are defined by the way we name them.
# Variables that start with $ have global scope
$var = "I'm a global var"
defined? $var #=> "global-variable"

# Variables that start with @ have instance scope
@var = "I'm an instance var"
defined? @var #=> "instance-variable"

# Variables that start with @@ have class scope
@@var = "I'm a class var"
defined? @@var #=> "class variable"

# Variables that start with a capital letter are constants
Var = "I'm a constant"
defined? Var #=> "constant"

# Class itself is also an object in ruby. So Class can have instance variables.
# Class variable is shared among the class and all of its descendants.

# base class
class Human
  @@foo = 0

  def self.foo
    @@foo
  end

  def self.foo=(value)
    @@foo = value
  end
end

#  derived class
class Worker < Human
end

Human.foo # 0
Worker.foo # 0

Human.foo = 2 # 2
Worker.foo # 2

# Class instance variable is not shared by the class's descendants.

class Human
  @bar = 0

  def self.bar
    @bar
  end

  def self.bar=(value)
    @bar = value
  end
end

class Doctor < Human
end

Human.bar # 0
Doctor.bar # nil

Challenge

What 2 words of code should be added below to give the Car both a getter and setter method for the variable :speed?

class Car
  #one line of code
end

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

Below is a Car class. Create a new "green" car below it, and accelerate its speed by 10, 20 and 40 units (with the Car's accelerate function).

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us