Lesson content is currently in draft form.
This chapter – and this entire tutorial, actually – tip-toes around the topic of object-oriented programming.
In Ruby, everything is considered an “object”. We’ve encountered at least two different objects: strings and numbers, also known as String and Fixnum, respectively.
We saw how they don’t mix so easily:
1 2 3 4 5 6 7 8 | |
Just so we don’t run into this TypeError problem again, how do we tell objects apart?
The dot operator
Try these commands in irb:
1 2 3 | |
You should see something like this:
1 2 3 4 5 6 | |
When we say that 1 is an object, we mean that it has properties and methods. In the example above, we accessed the class method of the 1 and "1" objects.
Think of class as the type of an object.
Take special note of the role of the period above. This is the dot operator (i.e. the period). That dot is a Ruby symbol used to invoke a method. In the above example, the dot operator is used to invoke the class method of the object, 1.
Methods
There are many more methods than just class. And methods are how we will accomplish some very useful tasks despite not doing much programming ourselves.
Every class of object has its own methods.
For example, the length method returns the number of characters in the invoking string:
1 2 | |
But the length method does not exist for numbers, because it doesn’t make sense to have that functionality:
1 2 3 | |
Methods return a value. The upcase method returns a string:
1 2 | |
This means you can chain together methods. Let’s find out the type of object that the string’s length method returns by invoking the class method, like so:
1 2 | |
If the result is a Fixnum (i.e. a number), then this is possible:
1 2 | |
Conversion methods
Here’s one method that will be particularly useful to us: the to_s method, which converts its invoking object to a string:
1 2 3 4 5 6 7 | |
There’s also a to_i method (the i stands for integer):
1 2 | |
We’ll be using many more methods in the chapters to come. The main takeaways are these:
- Every data type (i.e. class) has its own methods
- Methods return a value
- If you’re unsure of what a value’s datatype is, use its
classmethod to find out.
How do we know what methods exist? It’s a mix of looking up documentation and eventually memorizing the ones most helpful to you. Luckily, this is just a web search away for most methods.
Exercises:
What is the class of 42.2?
What is the resulting class of: 100 + 42.2 ?
Use addition and a conversion method to join these objects: "http://nottwitter.danwin.com/statuses/", "DarrellIssa", "/", 2, "/", "user_timeline.json"
Check out Ruby’s documentation of the String class for more String methods. See if you can find the method that replaces characters in a string with other characters.