You are previewing Ruby by Example.
O'Reilly logo
Ruby by Example

Book Description

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.

Table of Contents

  1. Ruby By Example: Concepts and Code
    1. Acknowledgments
    2. Introduction: What Is Ruby?
      1. Acquiring and Configuring Ruby
        1. On a Unix or Unix-like System
        2. On a Windows System
      2. Motivations for the Book
        1. Conventions
        2. Summary of Chapters
    3. 1. Interactive Ruby and the Ruby Environment
      1. Starting irb
      2. Using irb
        1. Expressions
        2. Everything Is an Object
        3. Integers, Fixnums, and Bignums
        4. Addition, Concatenation, and Exceptions
        5. Casting
        6. Arrays
        7. Booleans
        8. Flow Control
        9. Methods
        10. Variables
        11. Constants
      3. Using the Ruby Interpreter and Environment
    4. 2. Amusements and Simple Utilities
      1. #1 Is It Payday? (check_payday.rb)
        1. The Code
        2. How It Works
          1. Defining Constants
          2. Defining Variables
        3. The Results
      2. #2 Random Signature Generator (random_sig.rb and random_sig-windows.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      3. #3 The 99 Bottles of Beer Song (99bottles.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
      4. #4 Sound File Player (shuffle_play.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      5. Chapter Recap
    5. 3. Programmer Utilities
      1. #5 What Is Truth? (boolean_golf.rb)
        1. The Code
        2. How It Works
        3. Hacking the Script
        4. Running the Script
        5. The Results
      2. #6 Making a List (array_join.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. Hacking the Script
      3. #7 Command-Line Interface (uses_cli.rb and simple_cli.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. Hacking the Script
      4. #8 Palindromes (palindrome.rb and palindrome2.rb)
        1. The Code
        2. How It Works
        3. Hacking the Script
        4. Running the Script
        5. The Results
      5. Chapter Recap
    6. 4. Text Manipulation
      1. #9 End-of-Line Conversion (dos2unix.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      2. #10 Showing Line Numbers (line_num.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
      3. #11 Wrapping Lines of Text (softwrap.rb)
        1. The Code
        2. Running the Script
        3. The Results
        4. Hacking the Script
      4. #12 Counting Words in a File (word_count.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
      5. #13 Word Histogram (most_common_words.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      6. #14 Rotating Characters in a String (rotate.rb)
        1. The Code
        2. How It Works
          1. Recursion
        3. Running the Script
        4. The Results
      7. Chapter Recap
    7. 5. Number Utilities
      1. #15 Computing Powers (power_of.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
      2. #16 Adding Commas to Numbers (commify.rb)
        1. Inheritance
        2. Modules
        3. The Code
        4. How It Works
          1. The format_int Method
          2. The format_float Method
          3. Type Testing
        5. Running the Script
        6. The Results
      3. #17 Roman Numerals (roman_numeral.rb)
        1. The Code
        2. How It Works
          1. Class Variables
          2. Hash.merge
          3. More Recursion
          4. Multiples of Our Base
          5. Multiplying Strings by Integers
        3. Running the Script
        4. The Results
        5. Hacking the Script
      4. #18 Currency Conversion, Basic (currency_converter1.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      5. #19 Currency Conversion, Advanced (currency_converter2.rb)
        1. The Code
        2. How It Works
          1. Downloading Rates Information
          2. What If You Can’t Download New Rates?
        3. Running the Script
        4. The Results
        5. Hacking the Script
      6. Chapter Recap
    8. 6. Functionalism with Blocks and Procs
      1. #20 Our First lambda (make_incrementer.rb)
        1. The Code
        2. How It Works
        3. The Results
      2. #21 Using Procs for Filtering (matching_members.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
      3. #22 Using Procs for Compounded Filtering (matching_compound_members.rb)
        1. The Code
        2. How It Works
          1. Blocks, Procs, and the Ampersand
          2. Filtering with Each Proc via map
          3. Finding the Intersections with inject
          4. Array Intersections
        3. The Results
        4. Hacking the Script
      4. #23 Returning Procs as Values (return_proc.rb)
        1. The Code
        2. The Results
        3. How It Works
          1. Proc.call(args) vs. Proc[args]
          2. Using Procs as Blocks
          3. The inspect Method
      5. #24 Nesting lambdas
        1. The Code
        2. How It Works
      6. #25 Procs for Text (willow_and_anya.rb)
        1. The Code
          1. The Him Class
          2. The SameTimeSamePlace Class
          3. The willow_and_anya.rb Script
        2. How It Works
          1. The Him Class: Creating Procs with lambda
          2. The SameTimeSamePlace Class: Alternatives to lambda for Creating Procs
          3. Flexible Arity for Proc.new
          4. Blocks, Arguments, and yield
          5. Using Both Him and SameTimeSamePlace in willow_and_anya.rb
        3. Running the Script
        4. The Results
        5. Hacking the Script
      7. Chapter Recap
    9. 7. Using, Optimizing, and Testing Functional Techniques
      1. #26 Basic Factorials and Fibonaccis (factorial1.rb through fibonacci5.rb)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
          1. Using include? (factorial2.rb and fibonacci2.rb)
          2. Passing the returns1 or returns_self Array as an Argument (factorial3.rb and fibonacci3.rb)
          3. Making RETURNS1 or RETURNS_SELF a Class Constant (factorial4.rb and fibonacci4.rb)
          4. Memoization of Results (factorial5.rb and fibonacci5.rb)
      2. #27 Benchmarking and Profiling (tests/test_opts.rb)
        1. Benchmarking
        2. The Code
        3. How It Works
        4. Running the Script
        5. The Results
        6. Profiling
        7. Hacking the Script
      3. #28 Converting Temperatures (temperature_converter.rb)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
      4. #29 Testing temperature_converter.rb (tests/test_temp_converter.rb)
        1. The Code
        2. The Results
        3. How It Works
        4. Hacking the Script
      5. Chapter Recap
    10. 8. HTML and XML Tools
      1. #30 Cleaning Up HTML (html_tidy.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      2. #31 Counting Tags (xml_tag_counter.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      3. #32 Extracting Text from XML (xml_text_extractor.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      4. #33 Validating XML (xml_well_formedness_checker.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      5. Chapter Recap
    11. 9. More Complex Utilities and Tricks, Part I
      1. #34 Finding Codes in the Bible or Moby-Dick (els_parser.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      2. #35 Mutating Strings into Weasels (methinks.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      3. #36 Mutating the Mutation of Strings into Weasels (methinks_meta.rb)
        1. The Code
        2. How It Works
        3. Running the Script
        4. The Results
        5. Hacking the Script
      4. Chapter Recap
    12. 10. More Complex Utilities and Tricks, Part II
      1. #37 Overnight DJ (radio_player1.rb)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
      2. #38 Better Overnight DJ (radio_player2.rb)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
      3. #39 Numbers by Name (to_lang.rb)
        1. The Code
          1. representable_in_english.rb
          2. representable_in_spanish.rb
          3. to_lang.rb
        2. How It Works
          1. The Two Mixins
          2. The Main Code
        3. The Results
        4. Hacking the Script
      4. #40 Elegant Maps and Injects (symbol.rb)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
      5. Chapter Recap
    13. 11. CGI and the Web
      1. Common Gateway Interface
      2. Preparation and Installation
      3. #41 A Simple CGI Script (simple_cgi.rb)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
      4. #42 Mod Ruby (mod_ruby_demo.rhtml and mod_ruby_demo.conf)
        1. The Code
          1. mod_ruby_demo.conf
          2. mod_ruby_demo.rhtml
        2. How It Works
        3. The Results
        4. Hacking the Script
      5. #43 CSS Stylesheets, Part I (stylesheet.rcss)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
      6. #44 CSS Stylesheets, Part II (stylesheet2.rcss)
        1. The Code
        2. How It Works
        3. The Results
        4. Hacking the Script
      7. Chapter Recap
    14. 12. RubyGems and Rails Preparation
      1. RubyGems
        1. Installing RubyGems
        2. Using RubyGems
          1. Listing Installed and Installable Gems
          2. Installing Gems
          3. Updating Gems
          4. Learning More About RubyGems
      2. Rails Preparation
        1. What Is Rails?
        2. Other Options for Installing Rails
          1. Via Operating System Package Manager
          2. From Source
          3. Pre-Packaged
        3. Databases
        4. The Structure of a Rails Application
          1. Generating a Rails Application
          2. Viewing Your Rails Application
          3. Basics for Generating Applications
      3. Chapter Recap
    15. 13. A Simple Rails Project
      1. Creating the Application
        1. Initial Creation
        2. Preparing the Database
        3. Adding Data
        4. Creating the Model and Controllers
          1. Creating the Photo Model
          2. Creating the Album and Feed Controllers
      2. Dissecting the Application
        1. Dissecting the Photo Model
        2. Dissecting the Controllers
          1. Dissecting the Album Controller
          2. Dissecting the Application Controller
          3. Dissecting the Feed Controller
        3. Dissecting the Helpers
          1. Dissecting the Album Helper
          2. Dissecting the ApplicationHelper
          3. Dissecting the FeedHelper
          4. Dissecting the FooterHelper
        4. Dissecting the Album Controller’s Views
          1. Dissecting the index View
          2. Dissecting the show View
        5. Dissecting the Feed Controller’s images View
        6. Dissecting the Album Controller’s Layout
        7. Using CSS
      3. Using the Application
      4. Learning More About Rails
      5. Chapter Recap
    16. A. How Does Ruby Compare to Other Languages?
      1. C
      2. Haskell
      3. Java
      4. Lisp
      5. Perl
      6. PHP
      7. Python
      8. Smalltalk
      9. Summary of Ruby vs. Other Languages
    17. Index
    18. About the Author
    19. Colophon