Ruby by Example

Ruby by Example

Concepts and Code
by Kevin C. Baird
June 2007, 312 pp.
ISBN-13: 
9781593271480

There may be no better way to learn how to program than by dissecting real, representative examples written in your language of choice. Ruby by Example analyzes a series of Ruby scripts, examining how the code works, explaining the concepts it illustrates, and showing how to modify it to suit your needs. Baird's examples demonstrate key features of the language (such as inheritance, encapsulation, higher-order functions, and recursion), while simultaneously solving difficult problems (such as validating XML, creating a bilingual program, and creating command-line interfaces). Each chapter builds upon the previous, and each key concept is highlighted in the margin to make it easier for you to navigate the book.

You'll learn how to:

  • Use the interactive Ruby shell (irb) to learn key features of the language
  • Extend Ruby using RubyGems, the Ruby package manager
  • Create numerical utilities, as well as utilities that process and analyze HTML/XML
  • Implement purely functional and metaprogramming techniques to save time and effort
  • Optimize, profile, and test your code to make sure that it not only does its job, but does it well
  • Create web applications using Rails

Ruby is the fastest growing programming language today, and for good reason: Its elegant syntax and readable code make for prolific and happy programmers. But it can be difficult to understand and implement without a little help. Ruby by Example shows you how to take advantage of Ruby as you explore Ruby's fundamental concepts in action.

Author Bio 

Kevin C. Baird received his Ph.D. from the State University of New York at Buffalo. He originally wrote his dissertation in Python but rewrote the project after discovering Ruby, and he hasn't looked back since. He has presented at RubyConf and written articles for Linux Journal, Music & Computers magazine, and the New Interfaces for Musical Expression conference proceedings.

Table of contents 

Introduction: What is Ruby?
Chapter 1: Interactive Ruby and the Ruby Environment
Chapter 2: Amusements and Simple Utilities
Chapter 3: Programmer Utilities
Chapter 4: Text Manipulation
Chapter 5: Number Utilities
Chapter 6: Functionalism with Blocks and Procs
Chapter 7: Using, Optimizing, and Testing Functional Techniques
Chapter 8: HTML and XML Tools
Chapter 9: More Complex Utilities and Tricks Part I
Chapter 10: More Complex Utilities and Tricks Part II
Chapter 11: CGI and the Web
Chapter12: RubyGems and Rails Preparation
Chapter 13: A Simple Rails Project
Appendix: How Does Ruby Compare to Other Languages?
Index

View the detailed Table of Contents (PDF)
View the Index (PDF)

Reviews 

"All told, [Ruby by Example] is yet another great book from No Starch Press. I enjoyed reading it, and I sharpened my Ruby skills along the way."
LinuxWorld (Read More)

"Regardless of your leanings or preferences, if you believe learning Ruby is in your future, pick up a copy of this book. You'll start acquiring hands-on experience from the very beginning."
Linux Magazine

Kevin Baird, author of Ruby by Example, talks with Dr. Dobb's Journal about the Ruby language and its place among other high-level scripting languages.
Click here to listen to the podcast.

"The real subject matter [of Ruby by Example] is an introduction to Ruby from a functional programming perspective. This is a huge bonus. There are many books about Ruby that emphasize its OO features, but too few with a clear, succinct, powerful approach to functional programming."
Giles Bowkett (Read More)

"The book is aimed at being immediately accessible to all levels of Ruby programmers, however it has a very interesting slant towards the often overlooked functional programming strengths of Ruby. If you love your scripting languages served with a burst of lambdas and proc objects, or you're a fan of Haskell and Lisp, this book is for you."
LinuxSecurity.com (Read More)

"If you're looking for a good book to help build your Ruby muscles, this is a good one to grab."
Pat Eyler (Read More)

"There is something to be said for Baird's 'let's dive in and try it out' approach. If you want to get quickly started with some hands-on Ruby coding, Ruby By Example is the way to go."
bitwise magazine (Read More)

"This book is an excellent resource for improving one's understanding of Ruby and using it more effectively."
Major Keary, Book News

"If you've already got some programming experience under your belt and are searching for a book that'll get you up to speed with Ruby very quickly, I strongly recommend Ruby by Example (written by Kevin Baird and published by No Starch Press, whose books on coding have impressed me)."
Global Nerdy (Read More)

Updates 

Page 101:
The program make_incrementer.rb contains an error. Line 22, which reads

incrementer_proc.call(other_addend) + "\n"

should read

incrementer_proc.call(other_addend).to_s + "\n"

Page 106 should read:
"blocks are akin to 'partial' or 'unborn' Procs and need the lambda method to be made into full fledged Procs"

instead of:

"blocks are akin to 'partial' or 'unborn' blocks and need the lambda method to be made into full fledged Procs"

Page 130 should read:
"Our tests showed that the simple fibonacci1.rb took about 6.20 seconds to run 5 successive operations of calling fib on the number 30."

instead of:

"Our tests showed that the simple fibonacci1.rb took about 6.20 seconds to run 30 consecutive operations of calling fib on the numbers from one to five."

Script Update: Some users on Windows have experienced difficulty with the understand_args? method in simple_cli.rb from script #7, wherein the default condition is not triggered. If you experience this problem, try replacing the line

OPTIONS.keys.any? { |key| OPTIONS[key].include?(args[0])

with

(not args[0]) || OPTIONS.keys.any? { |key| OPTIONS[key].include?(args[0]) }

Thanks to Gene Temple for finding this bug and proposing a solution.

File Update: The original radio_player2.rb script has a small bug that reports incorrect start times for the files. Note: The .zip and .bz2 archives on the main page were updated to include this file on 9/19/07.

Page 90, "The Results":
The phrase "returned value from output_rates..." should read "returned value from puts"

Page 131, IRB Example:
The period after 'wall.empty?' should be removed in the execution example.

Page 248, under "Dissecting the ApplicationHelper":
Edit app/helpers/application.rb to match the following:" should read, "Edit app/helpers/application_helper.rb to match the following:"

Page 257:
In the last sentence under "Using CSS," the show.css stylesheet is mistakenly called public.css. The sentence should read, "The stylesheets master.css, show.css, and index.css are available for download at this book's website."