We can create an empty class in Ruby by using the following syntax:
1 2 3 4 5 |
|
Next, lets initialize our class with the initialize
method like so:
1 2 3 4 |
|
Our Animal class can now hold vairables, in the form of instance variables and class vairables. Instance variables always start with @
while class varaibles start with @@
. We have to make sure every Animal we create has a name by giving it @name
instance varaible. This way only the Animal class and instances of that class will have access to @name
.
1 2 3 4 5 |
|
Now we can create an instance of our Animal class by calling the new
method on Animal
class and passing an argument.
1
|
|
Class variables are attached to the entire class, not just an instance of the class. For instance we can set up a simple BankAccount class and use class variables to track how many instance of the class have been created like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class BankAccount
@@total_accounts = 0
def initialize
@@total_accounts += 1
end
def self.accounts_created
@@total_accounts
end
end
savings = BankAccount.new
current = BankAccount.new
inv = BankAccount.new
puts "Number of accounts created is #{BankAccount.accounts_created}."
# => Number of accounts created is 3.
So now everytime a new instance of BankAcoount is created @@total_accounts
is increamented by one. Simple right?
The above illustration also subtly introduced the concept of Class mehtods. These usually start with self
. In the context of the class self
referes to the current class, hence they can be called directed without creating an instance of a class.
1 2 3 4 5 6 7 8 |
|
Instance methods, on the other hand, can only be called by creating an instance of the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The def
keyword creates an instance method. Hence the only way info
method can be called is by creating an instance of Author
.