Ruby - Classes and Objects

Introduction

A class is the blueprint for an object.

It defines the data an object contains and the way it behaves.

The following class defines a dog:

class Dog 
   def set_name( aName ) 
      @myname = aName 
   end 
end 

The class definition begins with the keyword class.

The name of the class must begin with an uppercase letter.

The class contains a method called set_name.

This takes an incoming argument, aName.

The body of the method assigns the value of aName to a variable called @myname.

Related Topic