The book Programming Ruby, also commonly referred to as “the pickaxe book,” is widely regarded in the Ruby community as a must-have. Unfortunately, it doesn’t really meet my expectations. You can find parts of the book online, so you can see for yourself what I’m talking about.
Take a look at the chapter on Basic Input and Output. Let’s say you want to open a new text file and direct your output to that file. This seems pretty basic, and you’d expect to see it explained in a chapter that purports to cover input and output. On this seemingly important topic, readers are given one sentence and no examples:
The same methods that we’ve been using for “simple” I/O are available for all file objects.
Perhaps I am an atypical computer book consumer. If I’m in a hurry to get something done (and I’m always in a hurry), I find the right chapter in the book and skim the code samples to find one that illustrates how to do what I want to do, then I read the surrounding text. I’m not interested in reading the full chapter and drawing inferences, especially in cases like this where I know how to accomplish the same task in about ten other programming languages and I just need to know the syntax. Anyway, here’s the missing example:
f = File.open(“mynewfile.txt”, “w”) f.puts(“a line of text”) f.close
How difficult was that?
June 19, 2006 at 1:16 pm
That actually makes your point better than you may have realized… something like
File.open("mynewfile.txt", "w") do |f| f.puts("a line of text") endis rather more idiomatic. (It also arranges for the close to happen even if something in the middle throws an exception).
June 19, 2006 at 1:35 pm
O’Reilly’s cookbooks (at least the Perl and Java Cookboks) are fantastic for this sort of thing. Is someone slaving on a Ruby Cookbook?
June 19, 2006 at 3:54 pm
Poof! Ruby Cookbook
June 21, 2006 at 11:06 am
And, relatedly, Rails Recipes.
July 25, 2006 at 4:18 pm
FWIW the online version is the old edition of the book. Here is what the 2nd Edition has to say on the subject:
etc.