When I first started at Flatiron, I believed Ruby was the language of choice because of its simplicity–I might have even gone so far as to call it straightforward. I didn’t yet understand its eloquence–but I could, as a beginninger, tell that very quickly I was stumbling head-first into some pretty serious logic. I liked that it made me immediately feel smart. Like I was a real programmer! And so, for the first part of the course, I missed the heart of the language itself.
The heart. The truth. Ruby is alive: a world of objects, instances of Fixnum and String and Integer, and a thousand other classes upon which to build.
But they are all just objects.
Sandi Metz believes we should get to know our objects. We should talk to them. Ask them what they want, what they yearn for…so today, I start at the very beginning and ask myself: How is everything an object?
For starters, every Ruby object is an instance of some class.
Classes earn their keep by providing two key functions:
- Being containers for methods.
- Being factories for making instances.
And every class except one has a superclass, someplace that class can turn to when someone calls a methods the class doesn’t understand.
Let’s check it out by creating our very own CoolBeans class.
1 2 3 4 5 6 7 8 9 10 11 12
Just from looking at it, there are four readily apparent methods our class CoolBeans has:
- It can initialize a default empty array of beans when a new instance is created
- It can count how many beans it has
- It can return the object assigned to @beans (through the instance method beans)
- It can assign a new object to @beans (through the instance method beans=)
(The third and fourth methods are created through my attr_accessor)
But don’t trust me! Let’s just ask the class what instance methods it has!
First–lets make an instance of the class:
This returns an object that is an instance of CoolBeans. Our object variable, @beans,(also known as an instance variable), is set to its default value of an empty array upon initialization.
Second-what class is this object exactly?
Okay–so we knew that…but now, we can ask the class of this object, what instance methods it has:
But we don’t even need to go up to the class to know the instance methods because the object itself is an instance and knows its methods:
Wait a hot sec. I thought this class only had 4 instance methods? The ones listed earlier! So what exacty is happening here?
In Ruby if you can reference something with a variable–it’s an object, and virtually all Ruby objects can trace their ancestry back to a Basic Object.
1 2 3 4 5 6
Hence, almost everything in Ruby is an object–which works out quite conveniently for us, because everything being an object and such, Ruby has given us a shared set of methods all objects have in common, re: the extra 50 or so instance methods available to the class that are not written in the code itself, but rather inherited.
Here are some common methods all objects have–and you should know them if for no other reason than to stay out of their way 😎.
1 2 3 4 5
But one last thing! Where did our initialize method go? Why wasn’t it listed in my instance methods?
That is because instance_methods returns an array of public and protected methods. However, initialize is always private. To see it, we would have to do the following:
And there you have it. Front and center.