As we mentioned in previous chapters, a class is a description of objects. A particular object is an instantiation of the class, having a unique name selected for it by the programmers. As with all information in a computer system, the various classes are stored in files. When reading the previous chapters, you probably noticed the following syntax in some examples:
customer
=
String
.
new
This is essentially the syntax for instantiating a new object. The
example shows that you are instantiating an object whose class is String
. The object’s name is customer
, and the
name is used as a variable from the class String
. Now you can manipulate the customer
variable using the different methods learned in the preceding
chapters.
Ruby provides many built-in classes like strings and arrays, but you can also create your own class. User-defined classes are a great way to group and categorize something’s characteristics. For example, if you want to organize a database for bank accounts, you can create a class describing the properties and behaviors of each bank account.
Example 9-1 provides an outline for defining your own class.
1
class
Classname
2
def
initialize
(
var1
,
var2
,
.
.
.
,
varn
)
3
@variable_1
=
var1
4
@variable_2
=
var2
5
.
.
.
6
@variable_n
=
varn
7
end
8
9
def
method_1
10
# code
11
end
12
13
def
method_2
14
# code
15
end
16
end
We now describe lines 1–7 (the rest will be covered in the following
sections), which define a class and initialize variables local to the
class. To define a class, use the class
keyword followed by a descriptive name that characterizes an object. For
example, if you wanted to create a class for a bank account called
Account
, define the class by
typing:
class
Account
end
Another important keyword you should notice in the class definition
is def
(short for define). This keyword
is used to define new activities (called methods)
that may be performed on the object. This keyword is also used to define
the special method initialize
, which is called every time
a new instance of the class, that is, a new object, is created. All
classes have this special method, which is called a
constructor. We will explain class generation using
an example of a class describing bank accounts called Account
, the first iteration of which can be
seen in Example 9-2.
1
class
Account
2
def
initialize
(
balance
)
3
@balance
=
balance
4
end
5
end
The variables inside the parentheses after initialize
are the parameters that are assigned
when creating, or instantiating, the object.
Now when instantiating an object using the Account
class, the object will have a variable
called balance
with an initial value
that you can assign using a parameter. The special character (@
) is used to indicate that this is an
instance variable, meaning that it is a parameter of
the object. Variables might be specific to a single method in the class,
but these instance variables can be accessed by any method in the object
description. These instance variables are sometimes referred to
as storing the properties of an object.
We have just created a user-defined class, but how do you use it?
You can instantiate an object of the Account
class the same way you create new
strings and arrays:
bob
=
Account
.
new
(
10
.
00
)
Each created instance of a class will have its own unique instance variables. If a variable is prefixed with @@, it becomes a class variable that is shared across all instances of the class. We do not discuss class variables in this book.
This example creates an object called bob
of the Account
class. Remember when you created the
initialize
method? You assigned one
parameter called balance
; this is the
value in the parentheses. The parameter passed in the parentheses will
become the initial balance of Bob’s account.
What other variables should you consider adding to the Account
class? What kind of behaviors should the
class contain? In the following sections, we describe grouping data, as
well as adding your own methods and working with the data through the
object’s methods.
3.131.38.14