Programming Ruby 1.9: The Pragmatic Programmers' Guide (Facets of Ruby)
Dave Thomas, Chad Fowler
Format: PDF / Kindle (mobi) / ePub
Would you like to go from first idea to working code much, much faster? Do you currently spend more time satisfying the compiler instead of your clients or end users? Are you frustrated with demanding languages that seem to get in your way, instead of getting the work done? Are you using Rails, and want to dig deeper into the underlying Ruby language? If so, then we've got a language and book for you!
Ruby 1.9 is the latest version of Ruby. It has many compelling features, including multinationalization support, new block scoping, and better performance. At the same time, the current Ruby 1.8 is still being maintained and is widely used. We're therefore releasing both the Ruby 1.9 version of the PickAxe (this book) alongside the 1.8 version of the PickAxe. Readers can choose the book that matches their environment.
Ruby is a fully object-oriented language, much like the classic object-oriented language, Smalltalk. Like Smalltalk, it is dynamically typed (as opposed to Java or C++), but unlike Smalltalk, Ruby features the same conveniences found in modern scripting languages, making Ruby a favorite tool of intelligent, forward-thinking programmers and the basis for the Rails web framework.
This is the reference manual for Ruby, including a description of all the standard library modules, a complete reference to all built-in classes and modules (including all the new and changed methods introduced by Ruby 1.9). It also includes all the new and changed syntax and semantics introduced since Ruby 1.8. Learn about the new parameter passing rules, local variable scoping in blocks, fibers, multinationalization, and the new block declaration syntax, among other exciting new features.
Hashes and Arrays Let’s round off this section with a simple program that calculates the number of times each word occurs in some text. (So, for example, in this sentence, the word the occurs two times.) The problem breaks down into two parts. First, given some text as a string, return a list of words. That sounds like an array. Then, build a count for each distinct word. That sounds like a use for a hash—we can index it with the word and use the corresponding entry to keep a count. Let’s start
and (mostly) friendly as it ever was—that’s quite an achievement given the explosive growth we’ve enjoyed. For the third (tenth anniversary) printing, Wayne E. Seguin was kind enough to check the section on the wonderful tool RVM, and Luis Lavena checked the section on installing under Windows, as well as the chapter on running Ruby on Windows. And I’d like to call Anthony Burns a hero for doing an amazing job of reading through the changes as I was writing them, but that would take away from the
this to allow the method chaining in the line scores << 10 << 20 << 40. Because each call to << returns the scores object, you can then call << again, passing in a new score. As well as the obvious operators, such as +, *, and <<, indexing using square brackets is also implemented as a method call. When you write this: some_obj[1,2,3] you’re actually calling a method named [ ] on some_obj, passing it three parameters. You’d define this method using this: class SomeClass def (p1, p2, p3) # ...
1 b = 2 a, b = b, a Ruby lets you have a comma-separated list of rvalues (the things on the right of the assignment). Once Ruby sees more than one rvalue in an assignment, the rules of parallel assignment come into play. What follows is a description at the logical level: what happens inside the interpreter is somewhat hairier. Users of older versions of Ruby should note that these rules have changed in Ruby 1.9. 1.9 First, all the rvalues are evaluated, left to right, and collected into an
makes writing tests easy, and the tests make it easier to verify that your code is working. Once you get into the swing of it, you’ll find yourself writing a little code, writing a test or two, verifying that everything is copacetic, and then writing some more code. Prepared exclusively for Ian MacLeod C HAPTER 13. U NIT T ESTING Unit testing is also pretty trivial—run a program that calls part of your application’s code, get back some results, and then check the results are what you