To talk about instance methods in Ruby, you have to first talk about classes and objects. A Class
in ruby is a factory for making objects. What is an object? In, Ruby, it’s everything. 2 + 2
will return a Fixnum object, 'any strings'
will return a string object, true
and false
are boolean type objects. When you want to program anything with a bit of complexity, it becomes easier/necessary to use a Class
.
When you churn out an object created by a class, you are creating an instance of that class. It inherits all the methods defined by that class and a few more you may never have to use. Let’s try making a class and instantiating a new object. For this post, let’s make a Dog
and Person
class, and create a few instance methods that can be called on newly created objects by that class.
We’ll be coding our solution to the Instance Method Lab. Create a ruby file in your terminal. I’m on a macbook using osx so I’ll create a new ruby file with
We want to create our Dog
class. Let’s start by defining it in the dog.rb
file
Nice. We have a Dog
class, but new instances of Dog
wouldn’t have much to do. Let’s change that make our dogs be able to bark with instance methods. We define an instance method like this
Now if we want to make a dog bark, we’d instantiate a new dog with
fido.bark
would return "Woof!"
. How would you get fido.sit
to return 'The Dog is sitting'
? With our Person
class, we want our new person to respond to a #talk instance method.
Now, if we were to create a new instance of the Person class, dave = Person.new
, and then call the #talk instance method on the newly instantiated person dave.talk
, we’ll see the message 'Hello World!'
. How would you get our new person to respond to a #walk method that prints 'The Person is walking'
to the console?
Run your tests with rspec
in the terminal, after you coded out the rest of the instance methods!