Chapter 5 Classes

Class refers to a category of objects. Specific object belonging to a class is called an instance of that class. In creating an object in Ruby, define first the characteristic of a class and create an instance.

To define a class, put the class keyword at the beginning of a line, followed by < and the class it inherits from.

Example:

class ClassOne < object
end

Classes can include variables and methods. To start creating classes, add the initialize method first. Ruby calls the initialize method when you call the (class).new. Once the new method is called, Ruby creates a new object and calls the initialize method to setup the object’s state.


  • Currently 3.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
  Flag Inappropriate Content 0 comments