Now that we understand the difference between classes and instances, let's code a class.
Let's think about dogs. All dogs have a name and a breed. This means that these are attributes of dogs.
First, let's create a Dog class. Create a new file called dog.rb
and enter the following information:
class Dog
end
Congratulations! You've written your first Ruby class! Let's now create a new Dog
and print it out to the console. Change your file to look like this:
class Dog
end
barnie = Dog.new
puts barnie.inspect
Save the file and run the program. It should give you an output something like this:
#<Dog:0x000000008052c8>
The
inspect
method allows us to look at the object in more detail.
We just created our first Dog
object. Dog
is the class, and barnie
is an instance of the Dog
class.
Let's go through what we just did.
Dog
class (it doesn't do anything yet)Dog
class with the new
methodDog
into a variable called barnie
barnie
Let's add some code to store information inside each Dog
object. Change the file to look like this:
class Dog
def initialize(name, breed)
@name = name
@breed = breed
end
end
dog = Dog.new
puts dog.inspect
Save the file and run the program.
Woops! Looks like we got an error.
dog.rb:2:in `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)
from dog.rb:8:in `new'
from dog.rb:8:in `<main>'
Debugging
Let's try to decipher this error message:
dog.rb:2:in `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)
From this error message, we can tell where the bug is. First, it says that the error is on line 2 of
dog.rb
(dog.rb:2
). Then it says that the error is ininitialize
(dog.rb:2:in
initialize'). **Finally**, it says that we are giving a wrong number of arguments - we gave it 0 arguments and it expected 2 arguments (
wrong number of arguments (given 0, expected 2) (ArgumentError) `).From this information, we know that the root of the problem is on line 2 in the
initialize
method. But wait...what is thisinitialize
method?
initialize
For those with experience with other programming languages, the
initialize
method is a constructor. A constructor in programming is a special method that initializes the object of the class.Whenever a new object is created, Ruby looks for a method called
initialize
and runs the method. In other words, when thenew
method is triggered, Ruby looks for a method calledinitialize
in the class and executes it.For example, let's say we have a class called
Test
that looks like this:class Test def initialize puts "I've been initialized!" end end
Now when we create a new instance of this class, let's see what happens:
Test.new
The output will look like this:
I've been initialized!
Again, when we create a new instance of a class in Ruby, it finds the
initialize
method and executes it. In this case, ourinitialize
method simplyputs
the string "I've been initialized!".Let's go back to our order program. When we run
Dog.new
, Ruby finds theinitialize
method and runs the method. In theinitialize
method, we have two parameters:name
andbreed
.The initialize method here expects two parameters - if it doesn't receive two parameters, then the program will return an error.
The parameters for the
initialize
method are passed in from thenew
method:Dog.new("name", "breed") # "name" and "breed" are passed on to the initialize method as arguments
The parameters passed in through the
new
method are now passed into theinitialize method.
The parameter values are then stored into two variables,
@name
and@breed
. These variables that have a@
attached to the back of the name are called instance variables.
In the next lesson, let's talk about what instance variables are.