Buy this Book
Print Book $49.99 PDF $34.99 Read it Now!
Print Book £35.50
Add to UK Cart
Reprint Licensing
Ruby Cookbook

By Lucas Carlson, Leonard Richardson
First Edition  July 2006 
Pages: 906
Series: Cookbooks
ISBN 10: 0-596-52369-6 | ISBN 13: 9780596523695
starstarstarstarstar (Average of 6 Customer Reviews)

Buy 2 Get 1 Free Free ShippingGuarantee

Book description

From data structures and algorithms, to integration with cutting-edge technologies, the Ruby Cookbook has something for every programmer. When you need to solve a problem, don't reinvent the wheel: look it up in the Cookbook.


Full Description

Do you want to push Ruby to its limits? The Ruby Cookbook is the most comprehensive problem-solving guide to today's hottest programming language. It gives you hundreds of solutions to real-world problems, with clear explanations and thousands of lines of code you can use in your own projects.

From data structures and algorithms, to integration with cutting-edge technologies, the Ruby Cookbook has something for every programmer. Beginners and advanced Rubyists alike will learn how to program with:

  • Strings and numbers
  • Arrays and hashes
  • Classes, modules, and namespaces
  • Reflection and metaprogramming
  • XML and HTML processing
  • Ruby on Rails (including Ajax integration)
  • Databases
  • Graphics
  • Internet services like email, SSH, and BitTorrent
  • Web services
  • Multitasking
  • Graphical and terminal interfaces

If you need to write a web application, this book shows you how to get started with Rails. If you're a system administrator who needs to rename thousands of files, you'll see how to use Ruby for this and other everyday tasks. You'll learn how to read and write Excel spreadsheets, classify text with Bayesian filters, and create PDF files. We've even included a few silly tricks that were too cool to leave out, like how to blink the lights on your keyboard.

The Ruby Cookbook is the most useful book yet written about Ruby. When you need to solve a problem, don't reinvent the wheel: look it up in the Cookbook.

Post-purchase benefits:

Register your book | Submit Errata | Examples


Browse within this book

Cover | Table of Contents


No time right now?   Email This Page Email these links Customers who bought this book also bought:
Enterprise Integration
with Ruby
Read more
Programming Ruby
The Pragmatic Programmer's Guide, Second Edition
Read more
Rails Recipes
Read more
Ruby on Rails: Up and Running
Read more

No time right now?   Email This Page Email these links Customers interested in this book may also be interested in:
Book:
Ruby in a Nutshell
Read more
Book:
Perl Cookbook
Read more
Book:
Ruby on Rails: Up and Running
Read more
Podcast:
What Is Ruby on Rails

Read more
Article:
Rolling with Ruby on Rails, Part 2

Read more

Book details

First Edition: July 2006
Series: Cookbooks
ISBN: 0-596-52369-6
Pages: 906
Average Customer Reviews: starstarstarstarstar (Based on 6 Reviews)


Featured customer reviews

Write a Review


A really good book,  January 28 2007
Rating: StarStarStarStarStar
Submitted by Jeremiah   [Respond | View]

This book is a must-have for any programmer just discovering Ruby. The examples are straight forward and relevant. Combine this with a good Rails book and you have just what you need to get started on a serious project.


Ruby Cookbook Review,  December 21 2006
Rating: StarStarStarStarStar
Submitted by Vince W.   [Respond | View]

This review was originally published for RubyDC on Urbanpuddle.com:

Life is short, proclaims the authors of O'Reilly's Ruby Cookbook. You have real problems and this book is here to solve them, they go on. Weighing in at around 850 pages, there certainly is a chance that what ever problem you have could be addressed in this book. But is it all cartoon foxes or a tale with descriptions and plot twists worthy of reading in your bathrobe over a cup of chamomile? Thankfully no. In this book, O'Reilly delivers a densely packed tome filled with information, most of it unrelated, intended to solving real problems.

If you've never used a recipe book before, the premise is simple. They teach you how to put together code to create a particular effect or output. Reading tons of code on paper can be awfully boring but thankfully Ruby lends itself well to these types of books largely because it's a human readable language and packs a lot of functionality in very little space. For instance, you can expect:
<% 3 times.do %> yeah

Will output yeah yeah yeah as you would expect when reading
three times, do 'yeah'. In the Ruby Cookbook, authors Lucas Carlson and Leonard Richardson have divided the problems into these logical groups:

1. Strings
2. Numbers
3. Date & Time
4. Arrays
5. Hashes
6. Files & Directories
7. Code Blocks & Iterations
8. Objects & Classes
9. Modules & Namespaces
10. Reflections & Metaprogramming
11. XML & HTML
12. Graphics and Other File Formats
13. Databases & Persistence
14. Internet Services
15. Web Development: Ruby on Rails
16. Web Services and Distributed Programming
17. Testing, Debugging, Optimizing, and Documenting
18. Packaging and Distributing Software
19. Automating Tasks with Rake
20. Multitasking and Multithreading
21. User Interface
22. Extending Ruby with Other Languages
23. System Administration

As you can see there is a lot of information in this book. <tangent>As I was flipping through it I began to wonder how I could possibly make room in my brain for all this knowledge. If I absorbed it all I'd probably have to discard some pretty fundamental things like remembering to zip up my fly. Trust me, nobody wants that. My guess is that Mssrs. Carlson & Richardson are in similar predicaments and my thanks to go them and their Significant Others for potentially sacrificing valuable basic skills in order to bring you all this wonderful code. </tangent>

Each recipe in the book is divided into three sections: Problem, Solution, and Discussion. As you can infer, the problem gives a concise description of the issue that the Solution will then resolve. The Solution portion is where most of the code is and, like all O'Reilly books, they do a good job of separating the text from code so there's never any confusion. The Discussion is often the longest part of the recipe and contains not only details on the solution but the occasional alternative approach as well.

I can't say that each recipes contained in this book is staggeringly useful. For instance, I personally do not foresee ever needing 2.14: Doing Math with Roman Numbers. But I guess some would-be gladiators out there might find that helpful. But the recipes that I did find interesting were very valuable and made the price of the book well worthwhile. I particularly enjoyed the recipes on Classifying Text with a Bayesian Analyzer, Documenting Your Website, and Checking a Credit Card Checksum.

Other recipes, while not urgently required, can add a polish and professionalism to your code to set you apart from your peers. For instance there is a very simple recipe for wrapping text which you can use as an alternative to truncating:

def wrap(s, width=78)
s.gsub(/(.{1,#{width}})(\s+|Z)/, "\\1\n")
end

Using it like: puts wrap("This text is not too short to be wrapped.", 20)

I did find some solutions a little lacking. The Sending Mails with Rails recipe is different enough from the similar recipe in Chad Fowler's Rails Recipes book to make it useful. Neither solution independently helped me craft a newsletter function for my website but both combined gave me the knowledge I needed to write my code the way I needed it. Likewise, Generating PDF Files recommended using PDF-Writer which I found slow for the amount of fields (> 200) and records (> 500) that I need for my application. Based on a recommendation from a fellow RubyDC member, I will be checking out Ruport and RTex to see if it helps. Honestly, it could be that all solutions give me the same headaches.

Overall I was really impressed with the book. As with any recipes book, the utility is directly proportional to your need. Ruby Cookbook is no different there. It makes sense that, since each program is unique, some proposed solutions won't fit perfectly. Yet they will still otherwise give you a great launching point towards finding your own answer! Most importantly, as the types of applications you need to code change and as their complexity grow, you will find methods to avoid many of the usual speedbumps along the way within these pages.


Ruby Cookbook Review,  November 30 2006
Rating: StarStarStarStarStar
Submitted by Brian Anderson   [Respond | View]

I've looked at a couple programming “cookbooks” over the few years I've been programming, and none have ever really captured my attention. Most of them I found were lacking in useful recipes for novice programmers, or the recipes used an outdated code style or covered specific areas of a programming language. And with others, I just found it difficult to find useful tips or tricks.

The Ruby Cookbook however, provides fresh, easy to read recipes that are full of neat tips and awesome tricks. And it's all provided in an easy to read, and more importantly easy to find, format. Programmers from novice to highly experienced will find this book useful.

The book is laid out using general data types as chapters at first, then moves into more abstract topics such as code blocks and modules; databases and persistence; web services and task automation with Rake.

Start with the table of contents to learn about a specific area, or thumb your way through the index to find specific topics or that fast piece of code you need to complete your project. No matter how you use it, the Ruby Cookbook is a must have book for any Ruby or Ruby on Rails programmer.

Read all reviews


This Book is a Gem!,  October 03 2006
Rating: StarStarStarStarStar
Submitted by Dave Walz-Burkett   [Respond | View]

Sometimes, the difference between getting a project off the ground and watching it linger on the launchpad is finding an example of how to do something in code.

Perhaps you're working on a project and you'd like to send an email using Ruby. Maybe you'd like to know how to read and write zip files or create thumbnail images from full size graphics files. Sometimes cruising through the API documentation just doesn't quite give you enough info.

The Ruby Cookbook fills the void for a portable version of a Ruby code snippet search engine. For each example, there is a Problem description, a Solution section containing one or more chunks of Ruby code and a Discussion section for follow-up. Most examples also have a See Also section for cross reference.

A huge amount of material is covered. Topics range from simple stuff like strings, numbers, hashes, arrays and objects to more challenging subjects like code blocks, reflection, metaprogramming and multithreading. It has chapters on Internet services, Web services and distributed programming, and Web development with Ruby on Rails.

I'm only just skimming the surface here, this is a big book with 873 pages. It is quite simply packed with goodies. There is something in this book for Ruby programmers at every level. The book is well written and easy to read. You can download a zip file containing all of the code samples from the O'Reilly website.

If you want to learn Ruby and Ruby on Rails, there are three books that will help you more than anything else: Programming Ruby by Dave Thomas, Agile Web Development with Rails by Dave Thomas and David Heinemeier Hansson and the Ruby Cookbook by Lucas Carlson and Leonard Richardson.


A 'must -have' book,  August 05 2006
Rating: StarStarStarStarStar
Submitted by Satish Talim   [Respond | View]

This book is a 'must-have' for all the Ruby enthusiasts. I hope they put up a .zip file of all the programs here.


Excellent Book,  July 25 2006
Rating: StarStarStarStarStar
Submitted by Vinodh Kumar   [Respond | View]

Super book from Beginers to Advanced.


Media reviews

"I've lost count of the times I've googled for ages for some ruby trick and found it in the Cookbook in about 10 seconds. It's brilliant, and the discussion sections always highlight something useful. "
-- Ashley Moran, Business Analyst, Codeweavers.net, Comp.lang.ruby

"If you would like to become a serious Ruby hacker, don’t hesitate to buy this book. In my opinion it is absolutely worth every cent - and even more. My only problem is that there are no more recipes - however this is not a critique but rather a compliment: you simply can not get enough - not even from nearly 900 pages. One could argue that some things are missing or he would rather see this instead of that (I believe the authors themselves have had some tough time deciding these matters) - but I guess everyone agrees that the material which made it to the book is absolutely top-notch. 5 out of 5 stars - a great addition to anyone’s Ruby bookshelf. "
-- Peter Szinek, MSc, RubyRailways

"...906 pages of shiny, nearly perfect Ruby code with detailed explanations of everything, this book is a fantastic way to learn Ruby."
-- Joel Spolsky, Joel on Software

"This sort of book lives and dies by two criteria - the quality of the code and the usefulness of the recipe selection. Ruby Cookbook wins on both. The topics covered are wide and leave little, if any, part of the language unexplained. "
-- Tony Williams, Honestpuck.com

"The difference between [O'Reilly books] and the other books you own are the coffee stains and dog-eared pages found from cover to cover, not just the first few chapters...Granted, every solution to every problem isn't contained in the covers of this book. But there are solutions to a wide array of problems and the authors do such a good job in discussing each solution in detail that you should be able to find guidance for most issues you encounter."
-- Tim Kuntz, About.com

"One of the greatest challenges to Ruby development was the lack of coherent resource base that development teams can refer to. There are great resources online but these have been mostly snippets of information located in various places. Ruby Cookbook follows the O'Reilly cookbook tradition and has good reference touch points to developers. This book help launch a Ruby project worked on by multiple teams in separate locations. Having a common reference point greatly aided in the delivery of the project. Ruby is fun to work with and with the Ruby Cookbook, it makes it easier to deliver on a project."
-- Srihari Mailvaganam, Amazon.com

"It's an impressive work especially because of its tremendous sweep: whether you need string handling, file I/O, database programming, network programming, multitasking, or accessing BitTorrent, it's all covered in this book. At a $33 street price, the book is a steal, and most Ruby developers are likely to keep a copy beside their workstation."
-- Andrew Binstock, Binstock on Software

"You can put Ruby Cookbook to work instantly by identifying the problems you're facing and looking for their equivalents in the book. Even if your problem is not covered directly, chances are there are chapters in the book that are close enough that you can at least get a head start on an intelligently constructed solution...The Ruby Cookbook is an indispensable tool for current or prospective Ruby programmers, both as a means of learning the language by showing how it works, and as a desk reference to help out when you don't know how to deal with a particularly challenging problem. I'm glad to have this book on my shelf."
-- Jem Matzan, The Jem Report

"After playing with it through the examples in the Ruby Cookbook, I must admit Ruby won my respect in the usability department - and this came across through the book's usability...I ended up being impressed greatly with the book - the way it is organized allows a programmer to dive in and simply get stuff done...the book gets a KnowProSE.com 9 out of 10 for readability and usefulness--a difficult thing in the weight class it is in. A must have reference for those interested in Ruby, I'll happily recommend this book."
-- Taran Rampersad, KnowProSE.com

"Here is the definitive book on implementing Ruby, covering such important topics as strings and numbers, arrays and hashes, databases, graphics, and multitasking. The authors have put together an incredible collection of ready-to-use solutions, clearly documented, with all necessary code. Programmers can get a head start by simply dropping code from the book into their work."
-- Michael Kleper, The Kleper Report on Digital Publishing

"...a weighty, powerful web programming guide covering both basic and advanced functions...packed with reference insights."
-- James Cox, Midwest Book Review

"RC simply gives a lot of value. It's massive: with a page count approaching 900, the authors have plenty of room to explain the language. They've put a great deal of care into the recipes; there's an elaborate test framework, for example, that validates each one automatically. Even better than that, though, is the content targeted for the human reader rather than the Ruby interpreter...The range of RC is admirable...RC is definitely a book we're keeping in our library."
-- Cameron Laird, UnixReview.com

"I was tempted to think I wasn't ready for this book yet, as I don't have a firm Ruby foundation. But I'm glad I ignored that thought. For a beginner, this is a wealth of real code that does things that you will need to do in all your programs. String manipulation, number and date processing, etc. Those are the things that are fairly simple when you have experience, but stumbling blocks when you have to think about every step you take. For the intermediate and experienced coder, you'll find more advanced techniques like web services or multithreading your programs. In both cases (beginner or expert), a book like this will start to seed your imagination. Scanning through the recipes, you're sure to have one of those "ooohh... I didn't think about doing *that*!" moments."
-- Thomas Duff, Duffbert's Random Musings

"As you can see there is a lot of information in this book. As I was flipping through it I began to wonder how I could possibly make room in my brain for all this knowledge. If I absorbed it all I'd probably have to discard some pretty fundamental things...My guess is that Mssrs. Carlson & Richardson are in similar predicaments and my thanks to go them and their Significant Others for potentially sacrificing valuable basic skills in order to bring you all this wonderful code."
-- Vince Wadhwani, UrbanPuddle

"Once you’re familiar with Ruby basics and want specifics on how to perform a wide variety of tasks, this book is a great resource."
-- Michael Slater, Technology for Humans

"After attending CodeMash and sitting in on a few great sessions around both Ruby and Rails, I decided to start using Ruby as my primary scripting language and I made a commitment to play around some more with Rails. The best resource that I’ve had had in both of these situations are each respective cookbook from O’Reilly...it’s been wonderful just skimming the books and getting a grasp on the capabilities and common tasks for each one. I’ve found out a lot about how to effectively use Ruby and I’ve found many capabilities that I didn’t think that were possible (at least easily) with Rails. These books are both great resources, superbly written, and deserve a place on your bookshelf. "
-- Ben Carey, The Sherpa Project

"Will you have need for all 300 plus recipes in this cookbook? Probably not…today, but perhaps soon! With over 800 pages, there is something for everyone."
-- T. Michael Testi, Blogcritics.org

"It is not an introductory book for Ruby and honestly I used the references given in the book to start with Ruby, because I didn't know anything of the language. Later on, when I got some feeling about the language, I started to enjoy the book, because it is giving a lot of information in the discussions that goes widely over the initial given problem for understanding more of its philosophy. It's a big book, but it is also giving a lot of recipes to real-world problems. If I'm going to program something in Ruby, I will search a good starting point recipe, that will give me a hint to a good solution of the problems I'll encounter. I would recommend the book to everyone who programs in Ruby."
-- Bart Vrancken, JavaLobby

Hide extended reviews


See larger cover


"This sort of book lives and dies by two criteria - the quality of the code and the usefulness of the recipe selection. Ruby Cookbook wins on both. "
--Tony Williams, Honestpuck.com