May 2022
Beginner
352 pages
7h 5m
English
So far in this tutorial we’ve seen many examples of Ruby objects. In this chapter, we’ll learn how to use Ruby classes to make objects of our own, which have both attributes (data) and methods (functions) attached to them.
As we saw in Section 4.2, Ruby objects can be created (or instantiated) using the class name and the new constructor method:
>> String.new("Madam, I'm Adam.") => "Madam, I'm Adam." >> Time.new(1969, 7, 20, 20, 17, 40) => 1969-07-20 20:00:00 -0700
We can create a class of our own using three basic elements:
1. Use the class keyword to define the class.
2. Use the special initialize method to specify the behavior of new.
3. Use an attribute accessor ...
Read now
Unlock full access