Thuta Learning
AdvancedProgrammingbeginner

Classes & Objects

Relax. We'll talk through this in plain words — no textbook voice.

Class is a blueprint for an object, written with the class keyword. Object instances are created from a class using .new.

ruby
class Car
  def drive
    puts "The car is moving."
  end
end

my_car = Car.new
my_car.drive

Car class has a drive method inside it. Car.new creates a new Car object, and my_car.drive calls the method.

You should see
The car is moving.

Info

Always start Ruby class names with a capital letter, like Car or UserProfile.

Easy traps

  • Defining a class with a lowercase name like car causes a syntax error. Class names must follow constant naming rules.
Classes & Objects | Thuta Learning