
Saved by Harold T. Harper
Learn to Program
Saved by Harold T. Harper
As you can see, the argument is now required. After all, what is say_moo supposed to multiply "mooooooo..." by if you don’t give it an argument? Poor Ruby has no idea.
So, you def(ined) the method say_moo. (Method names, like variable names, almost always start with a lowercase letter.
One way you could think of this is that a class is kind of like a cookie cutter: every time you call new on that class, you get a new cookie that’s defined by the shape of that cookie cutter. You’d never try to take a bite out of a cookie cutter, because a cookie cutter isn’t a cookie. Similarly, a class is a different kind of thing from the object
... See moreYou might be thinking to yourself, “This is a lot like the loops from earlier.” Yep, it’s similar. One important difference is that the method each is simply that: a method. while and end (much like do, if, else, and all the other keywords) are not methods. They are a fundamental part of the Ruby language, like = and parentheses; they are kind of l
... See moreMethods That Take Procs When you pass a proc into a method, you can control when the proc is called, how many times it’s called, or even if it’s called at all.
The variable number_of_moos points to the argument passed in. I’ll say that again, because it’s a little confusing: number_of_moos is a variable that points to the argument passed in. So, if you type say_moo(3), the variable number_of_moos points to the value 3 when the method is being run.
Let’s start with a short example: 1: toast = Proc.new do 2: puts "Cheers!" 3: end 4: 5: toast.call 6: toast.call 7: toast.call <= Cheers! Cheers! Cheers!
The only one of these that might trick you is ||. In English, we often use “or” to mean “one or the other, but not both.” For example, I might say to my kids, ”For dessert, you can have pie or cake.” I did not mean they could have them both. A computer, on the other hand, uses || to mean “one or the other, or both.” (Another way of saying it is “at
... See moreYou can think of these as being “under the hood”: unless you’re an automobile mechanic, all you need to know is the gas pedal, the brake pedal, and the steering wheel. These are the public interface of your car. How your airbag knows when to deploy, on the other hand, is internal to the car; the typical user (driver) doesn’t need to know how that w
... See more