Chapter 6
More About Methods

So far we’ve seen a number of different methods—puts, gets, and so on. (Pop quiz: List all the methods we have seen so far! There are ten of them; the answer is below.) However, we haven’t really talked about what methods are.

I believe the technical definition is that “methods are things that do stuff.” If objects (such as strings, integers, and floats) are the nouns in the Ruby language, then methods are like the verbs. And, just like in English, you can’t have a verb without a noun to do the verb. For example, ticking isn’t something that just happens; a clock (biological or otherwise) has to do it. In English, we would say “The clock ticks.” In Ruby we would say clock.tick (assuming that clock was a Ruby object, of course, and one that could tick). Programmers might say we were “calling clock’s tick method” or that we “called tick on clock.” (This goes a long way toward explaining why we aren’t invited to many parties. We? They! Why they aren’t invited to many parties….)

Anyway, did you take the quiz? Good. Well, I’m sure you remembered the methods puts, gets, and chomp, since we just covered those. You probably also got our conversion methods, to_i, to_f, and to_s. But did you get the other four? Yeah? No? Why, it’s none other than our old arithmetic buddies: +, -, *, and /! (See, it’s stuff like that. Arithmetic buddies? Seriously, Chris?)

As I was saying, just as every verb needs a noun, every method needs an object. It’s usually easy to tell which object is performing the method. It’s what comes right before the dot, like in our clock.tick example or in 101.to_s. Sometimes, though, it’s not quite as obvious, as with the arithmetic methods. As it turns out, 5 + 5 is really just a shortcut way of writing 5.+ 5. For example:

puts(​'hello '​.+ ​'world'​)
puts((10.* 9).+ 9)
hello world
99

It isn’t very pretty, so we won’t ever write it like that; however, it’s important to understand what is really happening.

This also gives us a deeper understanding of why we can do 'pig'*5 but we can’t do 5*'pig': 'pig'*5 is telling 'pig' to do the multiplying, but 5*'pig' is telling 5 to do the multiplying. 'pig' knows how to make 5 copies of itself and add them all together; however, 5 will have a much more difficult time of making 'pig' copies of itself and adding them together.

And, of course, we still have puts and gets to explain. Where are their objects? In English, you can sometimes leave out the noun; for example, if a villain yells “Die!” the implicit noun is whomever he is yelling at. In Ruby, if I say puts 'to be or not to be', the implicit object is whatever object you happen to be in. But we don’t even know how to be in an object yet; we’ve always been inside a special object Ruby has created for us that represents the whole program. You can always see what object you are in by using the special variable self. Watch this:

puts self
main

If you didn’t entirely follow all of that, that’s OK. The important thing to get from all this is that every method is being done by some object, even if it doesn’t have a dot in front of it. If you understand that, then you’re all set.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.31.11