Let's continue working with classes using a Robot
example to explore how Ruby classes serve as blueprints for creating objects. Here's a basic class definition:
Ruby1class Robot 2end 3 4robot_instance = Robot.new
At this stage, the Robot
class is like an empty shell. It exists but doesn't know how to do anything. To make it functional, it needs attributes and methods.
A constructor is a special method that initializes an object when it's created. In Ruby, the constructor is the method named initialize
. It sets up—or constructs—our new objects with the necessary initial states.
Here, we upgrade the Robot
class with a constructor:
Ruby1class Robot 2 def initialize(name, color) 3 @name = name 4 @color = color 5 end 6end 7 8robot_instance = Robot.new("Robbie", "red") # Robbie, a red robot, is born!
In this case, the initialize
method gets automatically called when we create a new Robot
instance, setting the name
and color
attributes. It's always good practice to use constructors like initialize
to ensure each instance starts with the correct initial values.
Having one constructor is great, but what if we want more flexibility in setting up our robots? In Ruby, we can provide default values for parameters to offer similar functionality.
Here's a default color for our robots:
Ruby1class Robot 2 def initialize(name, color='grey') 3 @name = name 4 @color = color 5 end 6end 7 8robot_instance = Robot.new("Robbie", "red") # Red Robbie 9robot_instance2 = Robot.new("Bobby") # Grey Bobby, no color provided
With default parameters, color
becomes optional. When we don't specify it, the robot is 'grey' by default.
Class methods provide additional behaviors for our objects, acting like commands that define how objects interact and perform actions.
This Robot
class allows the robots to introduce themselves:
Ruby1class Robot 2 def initialize(name, color='grey') 3 @name = name 4 @color = color 5 end 6 7 def say_hello 8 puts "Hello, I am #{@name} and I am #{@color}." 9 end 10end 11 12robot_instance = Robot.new("Robbie", "red") 13robot_instance.say_hello # Robbie says: "Hello, I am Robbie and I am red."`
The say_hello
method allows our robot instance to interact and even communicate.
In Ruby, self
refers to the current instance of a class. Using self
within an instance method allows us to access the instance calling the method, which is especially useful for distinguishing between local variables and instance variables or calling other methods on the same object.
Ruby1class Robot 2 def initialize(name, color='grey') 3 @name = name 4 @color = color 5 end 6 7 def say_hello 8 puts "Hello, I am #{self.name} and I am #{self.color}." 9 end 10 11 attr_accessor :name, :color 12end 13 14robot_instance = Robot.new("Bolt", "blue") 15robot_instance.say_hello # Outputs: "Hello, I am Bolt and I am blue."
In this example, self
provides a clear reference to the name
and color
of the specific Robot
instance calling say_hello
.
Great job! You've refreshed and deepened your understanding of Ruby classes, constructors, and class methods, and learned how to use default parameters effectively. We also explored self
, which provides additional control and clarity when working with instance methods. Ruby’s syntax allows for clear and concise class definitions, enabling more dynamic and flexible object creation. Next, we’ll move on to hands-on tasks to apply these concepts to more complex scenarios. Keep up the good work!