Synopses & Reviews
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.
Perfect for programmers at all levels, "Ruby by Example" explores Ruby's functional power-inspired by Lisp-through the use of numerous practical and illustrative examples. The book includes short scripts, ranging from the simple to the complex, that do something interesting while also illustrating key concepts in Ruby. These scripts manipulate text, manipulate numbers (in ways that extend Ruby's numeric classes), and much more, all while showing readers how to use the language to extend itself. The author offers full explanations of how the provided examples work, as well as ideas for expanding and improving them. Readers learn about Ruby's abilities by experimenting with IRB (the interactive Ruby shell); RubyGems; and relatively high-level topics such as open classes, mixins, inheritance, and functional programming (both straightforward recursion and built-in methods like map, inject, and so on). An introduction to building Web applications with Ruby on Rails is included as well as how to get Ruby working within a Web server.
Perfect for programmers at all levels, this guide explores Rubys functional power through the use of numerous practical and illustrative examples. The book includes short scripts, ranging from the simple to the complex, while also illustrating key concepts in Ruby.
About the Author
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
Acknowledgments; Introduction: What Is Ruby?; Acquiring and Configuring Ruby; Motivations for the Book; Chapter 1: Interactive Ruby and the Ruby Environment; 1.1 Starting irb; 1.2 Using irb; 1.3 Using the Ruby Interpreter and Environment; Chapter 2: Amusements and Simple Utilities; 2.1 #1 Is It Payday? (check_payday.rb); 2.2 #2 Random Signature Generator (random_sig.rb and random_sig-windows.rb); 2.3 #3 The 99 Bottles of Beer Song (99bottles.rb); 2.4 #4 Sound File Player (shuffle_play.rb); 2.5 Chapter Recap; Chapter 3: Programmer Utilities; 3.1 #5 What Is Truth? (boolean_golf.rb); 3.2 #6 Making a List (array_join.rb); 3.3 #7 Command-Line Interface (uses_cli.rb and simple_cli.rb); 3.4 #8 Palindromes (palindrome.rb and palindrome2.rb); 3.5 Chapter Recap; Chapter 4: Text Manipulation; 4.1 #9 End-of-Line Conversion (dos2unix.rb); 4.2 #10 Showing Line Numbers (line_num.rb); 4.3 #11 Wrapping Lines of Text (softwrap.rb); 4.4 #12 Counting Words in a File (word_count.rb); 4.5 #13 Word Histogram (most_common_words.rb); 4.6 #14 Rotating Characters in a String (rotate.rb); 4.7 Chapter Recap; Chapter 5: Number Utilities; 5.1 #15 Computing Powers (power_of.rb); 5.2 #16 Adding Commas to Numbers (commify.rb); 5.3 #17 Roman Numerals (roman_numeral.rb); 5.4 #18 Currency Conversion, Basic (currency_converter1.rb); 5.5 #19 Currency Conversion, Advanced (currency_converter2.rb); 5.6 Chapter Recap; Chapter 6: Functionalism with Blocks and Procs; 6.1 #20 Our First lambda (make_incrementer.rb); 6.2 #21 Using Procs for Filtering (matching_members.rb); 6.3 #22 Using Procs for Compounded Filtering (matching_compound_members.rb); 6.4 #23 Returning Procs as Values (return_proc.rb); 6.5 #24 Nesting lambdas; 6.6 #25 Procs for Text (willow_and_anya.rb); 6.7 Chapter Recap; Chapter 7: Using, Optimizing, and Testing Functional Techniques; 7.1 #26 Basic Factorials and Fibonaccis (factorial1.rb through fibonacci5.rb); 7.2 #27 Benchmarking and Profiling (tests/test_opts.rb); 7.3 #28 Converting Temperatures (temperature_converter.rb); 7.4 #29 Testing temperature_converter.rb (tests/test_temp_converter.rb); 7.5 Chapter Recap; Chapter 8: HTML and XML Tools; 8.1 #30 Cleaning Up HTML (html_tidy.rb); 8.2 #31 Counting Tags (xml_tag_counter.rb); 8.3 #32 Extracting Text from XML (xml_text_extractor.rb); 8.4 #33 Validating XML (xml_well_formedness_checker.rb); 8.5 Chapter Recap; Chapter 9: More Complex Utilities and Tricks, Part I; 9.1 #34 Finding Codes in the Bible or Moby-Dick (els_parser.rb); 9.2 #35 Mutating Strings into Weasels (methinks.rb); 9.3 #36 Mutating the Mutation of Strings into Weasels (methinks_meta.rb); 9.4 Chapter Recap; Chapter 10: More Complex Utilities and Tricks, Part II; 10.1 #37 Overnight DJ (radio_player1.rb); 10.2 #38 Better Overnight DJ (radio_player2.rb); 10.3 #39 Numbers by Name (to_lang.rb); 10.4 #40 Elegant Maps and Injects (symbol.rb); 10.5 Chapter Recap; Chapter 11: CGI and the Web; 11.1 Common Gateway Interface; 11.2 Preparation and Installation; 11.3 #41 A Simple CGI Script (simple_cgi.rb); 11.4 #42 Mod Ruby (mod_ruby_demo.rhtml and mod_ruby_demo.conf); 11.5 #43 CSS Stylesheets, Part I (stylesheet.rcss); 11.6 #44 CSS Stylesheets, Part II (stylesheet2.rcss); 11.7 Chapter Recap; Chapter 12: RubyGems and Rails Preparation; 12.1 RubyGems; 12.2 Rails Preparation; 12.3 Chapter Recap; Chapter 13: A Simple Rails Project; 13.1 Creating the Application; 13.2 Dissecting the Application; 13.3 Using the Application; 13.4 Learning More About Rails; 13.5 Chapter Recap; How Does Ruby Compare to Other Languages?; C; Haskell; Java; Lisp; Perl; PHP; Python; Smalltalk; Summary of Ruby vs. Other Languages; Colophon;