You are previewing The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition.
O'Reilly logo
The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition

Book Description

Ruby is an agile object-oriented language, borrowing some of the best features from LISP, Smalltalk, Perl, CLU, and other languages. Its popularity has grown tremendously in the five years since the first edition of this book.

The Ruby Way takes a “how-to” approach to Ruby programming with the bulk of the material consisting of more than 400 examples arranged by topic. Each example answers the question “How do I do this in Ruby?” Working along with the author, you are presented with the task description and a discussion of the technical constraints. This is followed by a step-by-step presentation of one good solution. Along the way, the author provides detailed commentary and explanations to aid your understanding.

Coverage includes

•    An overview of Ruby, explaining terminology and basic principles

•    Operations on low-level data types (numbers, strings, regular expressions, dates)

•    The new regular expression engine (Oniguruma)

•    Internationalization (I18N) and message catalogs in Ruby

•    Operations on hashes, arrays, and other data structures such as stacks, trees, and graphs

•    Working with general I/O, files, and persistent objects

•    Database coverage including MySQL, SQLite, Oracle, DBI, and more

•    Ruby-specific techniques in OOP and dynamic programming

•    Graphical interfaces in Ruby (Tk, GTK+, Fox, and Qt)

•    Working with Ruby threads for lightweight multitasking

•    Everyday scripting and system administration in Ruby

•    Working with image files, PDFs, YAML, XML, RSS, and Atom

•    Testing, debugging, profiling, and packaging Ruby code

•    Low-level network programming and client-server interaction

•    Web development tools including Rails, Nitro, Wee, IOWA, and more

•    Working with distributed Ruby, Rinda, and Ring

•    Ruby development tools such as IDEs, documentation tools, and more

The source code for the book can be downloaded from

Hal Fulton has worked for over 15 years with variousforms of Unix, including AIX, Solaris, and Linux. He was first exposed to Ruby in 1999, and in 2001 he began work on the first edition of this book–the second Ruby book published in the English language. He has attendednumerous Ruby conferences and has given presentations at several of those, including the first European Ruby Conference. 

He has two degrees in computer science from the University of Mississippi and taught computer science for four years before moving to Austin, Texas to work as a contractor for variouscompanies, including IBM Austin.  Hal currently works at Broadwing Communications in Austin, Texas, maintaining a large data warehouse and related telecom applications, working daily with C++, Oracle, and, of course, Ruby.

Table of Contents

  1. Copyright
  3. Foreword
  4. Acknowledgments
  5. About the Author
  6. Introduction
  7. Ruby in Review
    1. An Introduction to Object Orientation
    2. Basic Ruby Syntax and Semantics
    3. OOP in Ruby
    4. Dynamic Aspects of Ruby
    5. Training Your Intuition: Things to Remember
    6. Ruby Jargon and Slang
    7. Conclusion
  8. Working with Strings
    1. Representing Ordinary Strings
    2. Representing Strings with Alternate Notations
    3. Using Here-Documents
    4. Finding the Length of a String
    5. Processing a Line at a Time
    6. Processing a Byte at a Time
    7. Performing Specialized String Comparisons
    8. Tokenizing a String
    9. Formatting a String
    10. Using Strings As IO Objects
    11. Controlling Uppercase and Lowercase
    12. Accessing and Assigning Substrings
    13. Substituting in Strings
    14. Searching a String
    15. Converting Between Characters and ASCII Codes
    16. Implicit and Explicit Conversion
    17. Appending an Item Onto a String
    18. Removing Trailing Newlines and Other Characters
    19. Trimming Whitespace from a String
    20. Repeating Strings
    21. Embedding Expressions Within Strings
    22. Delayed Interpolation of Strings
    23. Parsing Comma-Separated Data
    24. Converting Strings to Numbers (Decimal and Otherwise)
    25. Encoding and Decoding rot13 Text
    26. Encrypting Strings
    27. Compressing Strings
    28. Counting Characters in Strings
    29. Reversing a String
    30. Removing Duplicate Characters
    31. Removing Specific Characters
    32. Printing Special Characters
    33. Generating Successive Strings
    34. Calculating a 32-Bit CRC
    35. Calculating the MD5 Hash of a String
    36. Calculating the Levenshtein Distance Between Two Strings
    37. Encoding and Decoding base64 Strings
    38. Encoding and Decoding Strings (uuencode/uudecode)
    39. Expanding and Compressing Tab Characters
    40. Wrapping Lines of Text
    41. Conclusion
  9. Working with Regular Expressions
    1. Regular Expression Syntax
    2. Compiling Regular Expressions
    3. Escaping Special Characters
    4. Using Anchors
    5. Using Quantifiers
    6. Positive and Negative Lookahead
    7. Accessing Backreferences
    8. Using Character Classes
    9. Extended Regular Expressions
    10. Matching a Newline with a Dot
    11. Using Embedded Options
    12. Using Embedded Subexpressions
    13. Ruby and Oniguruma
    14. A Few Sample Regular Expressions
    15. Conclusion
  10. Internationalization in Ruby
    1. Background and Terminology
    2. Coding in a Post-ASCII World
    3. Using Message Catalogs
    4. Conclusion
  11. Performing Numerical Calculations
    1. Representing Numbers in Ruby
    2. Basic Operations on Numbers
    3. Rounding Floating Point Values
    4. Comparing Floating Point Numbers
    5. Formatting Numbers for Output
    6. Formatting Numbers with Commas
    7. Working with Very Large Integers
    8. Using BigDecimal
    9. Working with Rational Values
    10. Matrix Manipulation
    11. Working with Complex Numbers
    12. Using mathn
    13. Finding Prime Factorization, GCD, and LCM
    14. Working with Prime Numbers
    15. Implicit and Explicit Numeric Conversion
    16. Coercing Numeric Values
    17. Performing Bit-level Operations on Numbers
    18. Performing Base Conversions
    19. Finding Cube Roots, Fourth Roots, and so on
    20. Determining the Architecture’s Byte Order
    21. Numerical Computation of a Definite Integral
    22. Trigonometry in Degrees, Radians, and Grads
    23. More Advanced Trigonometry
    24. Finding Logarithms with Arbitrary Bases
    25. Finding the Mean, Median, and Mode of a Data Set
    26. Variance and Standard Deviation
    27. Finding a Correlation Coefficient
    28. Generating Random Numbers
    29. Caching Functions with memoize
    30. Conclusion
  12. Symbols and Ranges
    1. Symbols
    2. Ranges
    3. Conclusion
  13. Working with Times and Dates
    1. Determining the Current Time
    2. Working with Specific Times (Post-epoch)
    3. Determining the Day of the Week
    4. Determining the Date of Easter
    5. Finding the Nth Weekday in a Month
    6. Converting Between Seconds and Larger Units
    7. Converting To and From the Epoch
    8. Working with Leap Seconds: Don’t!
    9. Finding the Day of the Year
    10. Validating a Date/Time
    11. Finding the Week of the Year
    12. Detecting Leap Years
    13. Obtaining the Time Zone
    14. Working with Hours and Minutes Only
    15. Comparing Date/Time Values
    16. Adding Intervals to Date/Time Values
    17. Computing the Difference in Two Date/Time Values
    18. Working with Specific Dates (Pre-epoch)
    19. Interconverting Between Time, Date, and DateTime
    20. Retrieving a Date/Time Value from a String
    21. Formatting and Printing Date/Time Values
    22. Time Zone Conversions
    23. Determining the Number of Days in a Month
    24. Dividing a Month into Weeks
    25. Conclusion
  14. Arrays, Hashes, and Other Enumerables
    1. Working with Arrays
    2. Working with Hashes
    3. Enumerables in General
    4. Conclusion
  15. More Advanced Data Structures
    1. Working with Sets
    2. Working with Stacks and Queues
    3. Working with Trees
    4. Working with Graphs
    5. Conclusion
  16. I/O and Data Storage
    1. Working with Files and Directories
    2. Performing Higher-Level Data Access
    3. Using KirbyBase
    4. Connecting to External Databases
    5. Conclusion
  17. OOP and Dynamic Features in Ruby
    1. Everyday OOP Tasks
    2. More Advanced Techniques
    3. Working with Dynamic Features
    4. Conclusion
  18. Graphical Interfaces for Ruby
    1. Ruby/Tk
    2. Ruby/GTK2
    3. FXRuby (FOX)
    4. QtRuby
    5. Other GUI Toolkits
    6. Conclusion
  19. Threads in Ruby
    1. Creating and Manipulating Threads
    2. Synchronizing Threads
    3. Conclusion
  20. Scripting and System Administration
    1. Running External Programs
    2. Command-Line Options and Arguments
    3. The Shell Library
    4. Accessing Environment Variables
    5. Scripting in Microsoft Windows
    6. The Windows One-Click Installer
    7. Libraries You Need to Know About
    8. Working with Files, Directories, and Trees
    9. Miscellaneous Scripting Tasks
    10. Conclusion
  21. Ruby and Data Formats
    1. Parsing XML with REXML
    2. Working with RSS and Atom
    3. Manipulating Image Data with RMagick
    4. Creating PDF Documents with PDF::Writer
    5. Conclusion
  22. Testing and Debugging
    1. Testing with Test::Unit
    2. The ZenTest Tools
    3. Using the Ruby Debugger
    4. Using irb As a Debugger
    5. Measuring Code Coverage
    6. Measuring Performance
    7. Prettyprinting Objects
    8. Conclusion
  23. Packaging and Distributing Code
    1. Using RDoc
    2. Installation and Packaging
    3. RubyForge and the RAA
    4. Conclusion
  24. Network Programming
    1. Network Servers
    2. Network Clients
    3. Conclusion
  25. Ruby and Web Applications
    1. CGI Programming with Ruby
    2. Using FastCGI
    3. Ruby on Rails
    4. Web Development with Nitro
    5. An Introduction to Wee
    6. Web Development with IOWA
    7. Ruby and the Web Server
    8. Conclusion
  26. Distributed Ruby
    1. An Overview: Using drb
    2. Case Study: A Stock Ticker Simulation
    3. Rinda: A Ruby Tuplespace
    4. Service Discovery with Distributed Ruby
    5. Conclusion
  27. Ruby Development Tools
    1. Using RubyGems
    2. Using Rake
    3. Using irb
    4. The ri Utility
    5. Editor Support
    6. Integrated Development Environments
    7. Conclusion
  28. The Ruby Community
    1. Web Resources
    2. Newsgroup and Mailing Lists
    3. Blogs and Online Magazines
    4. Ruby Change Requests
    5. IRC Channels
    6. Ruby Conferences
    7. Local Ruby Groups
    8. Conclusion
  29. Index