REVIEW - Ruby Cookbook 2e


Ruby Cookbook 2e


Lucas Carlson & Leonard Richardson




O’Reilly (2015)




Ian Bruntlett


March 2018



Before attempting to read this book, I’d recommend reading and understanding The Ruby Programming Language first.

This is the first O’Reilly Cookbook I have read from cover to cover. It has 25 chapters on a variety of subjects. It has flaws – ranging from daft (e.g. ignoring Array#to_h and writing it from scratch) to wrong (e.g. its description of the <=> operator in 5.5 ‘Sorting an array’). It is big – just over 970 pages long. It looks like this edition is orphaned – the online errata is sparse and O’Reilly stated in an e-mail that the sample source code would not be available for download – which is a pity – O’Reilly’s former reputation for maintaining errata and making examples available for download was a significant factor for me when I bought this book.

Chapter 1: Ruby 2.1. Discusses the changes made to Ruby between version 1.8 and 2.1.

The remaining chapters can be grouped by overall topic:

  • Built-in data structures (6 chapters)
  • Ruby idioms & philosophy (4 chapters)
  • Popular ways of storing data (3 chapters)
  • Network applications (4 chapters)
  • Programming support (3 chapters)
  • Miscellany (4 chapters)

Chapter 2 (19 Recipes): Strings. Deals with the obvious and introduces the use of irb (interactive Ruby) which is used heavily throughout the book. It covers many things that will augment the knowledge of someone with a basic grasp of Ruby strings.

Chapter 3 (17 Recipes): Numbers. Covers many things. From the obvious (converting strings to integers, comparing floating-point numbers) to more specialist stuff (matrices, logarithms).

Chapter 4 (14 Recipes): Date and Time. There are three classes in Ruby that handle time – the Time class (an interface to the C time libraries) and the Ruby classes ( Date and DateTime ). It handles the measurement of time, formatted output, Daylight Saving Time, running a code block periodically, waiting for time to elapse and adding a time-out to a long-running operation.

Chapter 5 (15 Recipes): Arrays. Starts off with the obvious (iterating over an array, building a hash from an array – note, use Ruby’s Array#to_h ). In 5.5 ‘Sorting an array’ there is a seriously wrong explanation of the <=> operator’s behaviour.

Chapter 6 (15 Recipes): Hashes. This is an interesting chapter both for people who have encountered hashes / dictionaries / associative arrays in other languages and those who are seeing them for the first time.

Chapter 7 (21 Recipes): Files and Directories. Covers day-to-day handling of them at a fairly high-level and O.S. independent perspective. Dig into the topic deep enough and you will uncover the underlying C / UNIX foundations.

Chapter 8 (11 Recipes): Code blocks and Iterations. This is fundamental to the idiomatic use of Ruby and this chapter is full of useful, pertinent examples. I’d read The Ruby Programming Language first, though.

Chapter 9 (19 Recipes): Objects and Classes. Good examples but best read by an intermediate Ruby programmer rather than a novice.

Chapter 10 (10 Recipes): Modules and Namespaces. Another interesting chapter but let down by a poor online errata. Some of its examples are useful – others are clever but fraught with pitfalls.

Chapter 11 (16 Recipes): Reflection and Metaprogramming. This is an interesting and challenging chapter, ranging from finding an object’s class through to quite involved meta-programming (Aspect-Oriented Programming and enforcing software contracts).

Chapter 12 (15 Recipes): XML and HTML. The preface of this book states that this book has ‘copy-and-paste code snippets’. This chapter explodes that myth. Consider 12.1 ‘Checking that XML is well-formed’. It casually uses the method assert_nothing_thrown without even telling the reader that this method belongs to a testing framework or where to find out more about it (it is discussed in Chapter 19 ‘Testing, Debugging, Optimizing and Documenting’).

Chapter 13 (14 Recipes): Graphics and Other File Formats. Has recipes for graphics, graphs, sparklines, text (encryption, csv file handling), compressed and compressing files, YAML, PDFs and MIDI files.

Chapter 14 (17 Recipes): Databases and Persistence. Covers RDBMSs – from database-specific bindings (MySQL, Postgres) to generic RDBMS support (DBI – an idea borrowed from Perl) and an accessing an RDBMS without using SQL (ActiveRecord). A plethora of other approaches ( YAML, Marshal, Madeleine, SimpleSearch, Ferret (inspired by Java’s Lucene library), Berkeley DB. The chapter finishes with a section on avoiding SQL injection attacks.

Chapter 15 (20 Recipes): Internet Services. I am no expert in networking but this chapter covers a lot of ground and supports just about everything I’ve ever wanted to do on a network and the Internet.

Chapter 16 (23 Recipes): Web development: Ruby on Rails. I have dabbled with web development, previously, studying Learning PHP, MySQL, JavaScript, CSS & HTML5 (3rd Edition) . After that, I looked around for something else to study. Rails was the killer app that attracted me to Ruby. This chapter covers a lot of Rails including basics, web application patterns and use of tools. For me, it provides a taste of things to come. I’ll return to this chapter after reading a dedicated Rails tutorial.

Chapter 17 (12 Recipes): Sinatra. Sinatra is a slim web framework. I don’t know enough about this sort of thing but this is another chapter that I will return to.

Chapter 18 (16 Recipes): Web Services and Distributed Programming. The previous chapters dealt with network programming (where you write software to enable people to do stuff on a network / the Internet). This chapter deals with distributed programming – where you write software to enable different computers to cooperate on a network / the Internet.

Chapter 19 (14 Recipes): Testing, Debugging, Optimizing and Documenting. This is an introduction to many useful things. I think there is the occasional typing error but I am not experienced enough in Ruby to decide that.

Chapter 20 (8 Recipes): Packaging and Distributing Software. This chapter is quite an eye-opener – Ruby has a lot of infrastructure for packaging and distributing software – typically in the form of gems. This is a particularly useful chapter.

Chapter 21 (8 Recipes): Automating tasks with Rake. I think Rake is a contraction of ‘Ruby Make’ and it performs so many repetitive tasks for Ruby programmers that it could be called the Ruby Butler. Like make , Rake is flexible and can be used for things beyond Ruby software development.

Chapter 22 (11 Recipes): Multitasking and Multithreading. Deals with threads, processes, Windows services, synchronising object access, and remote execution of code (via the net-ssh gem) and discusses various ‘gotchas’ that can trap the unwary. In places this chapter has been updated for Ruby 2.1

Chapter 23 (16 Recipes): User Interface. Covers the terminal / console / text UI, graphical UI, (with Tk, wxRuby, Ruby/GTK, AppleScript) and command line arguments using optparse. Other chapters cover Internet user interfaces. I didn’t try all of the examples. This chapter can be a bit bleeding edge. I couldn’t get the curses examples to run because support for it has been moved out of the Standard Library and I couldn’t persuade the relevant gem to install.

Chapter 24 (5 Recipes): Extending Ruby with other languages. This has three recipes about accessing C functions in shared object files but also covers writing C code inline in a Ruby script. It also covers accessing Java class libraries from within a Ruby script.

Chapter 25 (11 Recipes): System Administration. This covers system administration tasks being performed by Ruby in Windows and UNIX-like systems. Personally, I believe that these tasks might be better solved by writing a script in bash or PowerShell – but, as a piece of software becomes more and more complex, Ruby becomes more appealing.

Conclusion. Software changes. Things break. This book won’t protect you from that but the breadth of Ruby Programming knowledge here has proven to be useful and for that I am grateful.

VERDICT: Recommended.

Book cover image courtesy of Open Library.