11. Beyond This Tutorial
So, where can we go now? If you have a question, who can you ask? What if you want your program to open a webpage, send an email, or resize a digital photo? Well, there are many, many places to find help for Ruby. Unfortunately, that answer isn't very useful, is it? :-)
For me, there are really only three places I look for help with Ruby. If it's a small question and I think I can experiment on my own to find the answer, I use irb. If it's a bigger question, I look in my Pickaxe. And if I just can't handle it, then I ask for help on the ruby-talk mailing list.
IRB: Interactive Ruby
If you installed Ruby, then you installed irb. To use it, just go to your command prompt and type irb. When you are in irb, you can type any ruby expression you want, and it will return its value. Type 1 + 2, and it will return 3. (Note that you don't need to use puts). It's kind of like a giant Ruby calculator. When you are done, simply type exit.
There is much more to irb than this, but you can learn all about it in the Pickaxe.
The Pickaxe: "Programming Ruby"
The Ruby book you absolutely cannot miss is "Programming Ruby, The Pragmatic Programmer's Guide", by Andrew Hunt and David Thomas (the Pragmatic Programmers). While I strongly recommend the 2nd edition of this excellent book, covering all the latest Ruby features, you can also get a slightly older (but still relevant) free online version. (In fact, if you installed the Windows version of Ruby, you already have it).
You can find practically everything about Ruby, from basic to advanced, in this book. It's easy to read; it's comprehensive; it's almost perfect. I wish every programming language had a book of this caliber. In the back of the book, you will find a huge section detailing every method of every class, explaining it and giving examples. I simply love this book!
There are countless places where you can get it (including the Pragmatic Programmers' own site), but my favorite place is at ruby-doc.org. This version has a nice index, as well as a cross-reference (ruby-doc.org has lots of other great documentation, such as the Core API and the Standard Library... Basically, it documents everything that comes with Ruby. Check it out.).
And why is it called "the Pickaxe"? Well, there is a picture of a pickaxe on the cover of the book. It's a silly name, I guess, but it stuck.
Ruby-Talk: A Ruby Mailing List
Even with irb and the Pickaxe, sometimes you still might not be able to figure it out on your own. Or maybe you want to know if anyone has already done what you are doing, to see if you can use it. In these cases, the best place is ruby-talk, the Ruby mailing list. It is full of friendly, smart, helpful people. To learn more about it, or to subscribe, look here.
WARNING: There is a large volume of email on the list every day. I created a rule in my email client so it doesn't all stay in the same folder. But if you don't want to have to do that, you don't have to! The ruby-talk mailing list is mirrored on the comp.lang.ruby newsgroup, so you can see the messages there. In short, you will see the same messages, but in a slightly different way.
Tim Toady
I have been trying to protect you from something you will run into soon, it is the concept of TMTOWTDI (pronounced "Tim Toady"): There's More Than One Way To Do It.
Now some will tell you that TMTOWTDI is a wonderful thing, while others feel quite differently. I don't really have strong feelings on the subject in general, but I think it's a terrible way to teach someone how to program. (As if learning one way to do something wasn't challenging and confusing enough!).
However, now that you are moving beyond this tutorial, you will see a lot of diverse code. For example, I can think of at least five other ways to create a string (besides surrounding some text with single quotes), and each of them works a little differently. I only showed you the simplest of the six.
And when we talked about branching, I showed you if, but I didn't show you unless. I'll let you figure that out in irb.
Another nice shortcut you can use with if, unless, while, is the nifty one-line version:
# These words are from a program I wrote to generate
# English gibberish. Cool, huh?
puts 'grobably combergearl kitatently thememberate' if 5 == 2**2 + 1**1
puts 'enlestrationshifter supposine follutify blace' unless 'Chris'.length == 5
And finally, there is another way to write methods that take blocks (not procs). We saw this where we grabbed the block and turned it into a proc using the &block trick in the parameter list when defining the function. Then, to call the block, you use block.call. Well, there is a shorter way (although personally I find it more confusing). Instead of this:
def doTwice(&block)
block.call
block.call
end
doTwice do
puts 'murditivent flavitemphan siresent litics'
end
...you do this:
def doTwice
yield
yield
end
doTwice do
puts 'buritiate mustripe lablic acticise'
end
I don't know... What do you think? Maybe it's just me, but... yield?! If it was something like call_the_hidden_block or something like that, it would make a lot more sense to me. Many people say that yield makes sense to them. But I guess that's the purpose of TMTOWTDI: they do it their way, and I'll do it mine.
THE END
Use it for good and not for evil. :-) And if you found this tutorial helpful (or confusing, or if you found an error), let me know!