Book description
For more than a decade, Ruby developers have turned to The Ruby Way for reliable “how-to” guidance on effective Ruby programming. Now, Hal Fulton and André Arko have thoroughly updated this classic guide to cover new language enhancements and developers’ experiences through Ruby 2.1.
The new edition illuminates Ruby 2.1 through 400+ examples, each answering the question: “How do I do this in Ruby?” For each example, they present both a task description and realistic technical constraints. Next, they walk step-by-step through presenting one good solution, offering detailed explanations to promote deeper understanding.
Conveniently organized by topic, The Ruby Way, Third Edition makes it easier than ever to find the specific solution you want—and to write better code by reflecting Ruby’s unique philosophy and spirit.
Coverage includes
All source code for this book may be downloaded at www.rubyhacker.com.
informit.com/aw
informit.com/ruby
rubyhacker.com/therubyway
therubyway.io
Table of contents
- About This eBook
- Title Page
- Copyright Page
- Praise for The Ruby Way, Third Edition
- Praise for Previous Editions of The Ruby Way
- Dedication Page
- Contents
- Foreword
- Acknowledgments
- About the Authors
- Introduction
- Chapter 1. Ruby in Review
-
Chapter 2. Working with Strings
- 2.1 Representing Ordinary Strings
- 2.2 Representing Strings with Alternate Notations
- 2.3 Using Here-Documents
- 2.4 Finding the Length of a String
- 2.5 Processing a Line at a Time
- 2.6 Processing a Character or Byte at a Time
- 2.7 Performing Specialized String Comparisons
- 2.8 Tokenizing a String
- 2.9 Formatting a String
- 2.10 Using Strings as IO Objects
- 2.11 Controlling Uppercase and Lowercase
- 2.12 Accessing and Assigning Substrings
- 2.13 Substituting in Strings
- 2.14 Searching a String
- 2.15 Converting Between Characters and ASCII Codes
- 2.16 Implicit and Explicit Conversion
- 2.17 Appending an Item onto a String
- 2.18 Removing Trailing Newlines and Other Characters
- 2.19 Trimming Whitespace from a String
- 2.20 Repeating Strings
- 2.21 Embedding Expressions within Strings
- 2.22 Delayed Interpolation of Strings
- 2.23 Parsing Comma-Separated Data
- 2.24 Converting Strings to Numbers (Decimal and Otherwise)
- 2.25 Encoding and Decoding rot13 Text
- 2.26 Encrypting Strings
- 2.27 Compressing Strings
- 2.28 Counting Characters in Strings
- 2.29 Reversing a String
- 2.30 Removing Duplicate Characters
- 2.31 Removing Specific Characters
- 2.32 Printing Special Characters
- 2.33 Generating Successive Strings
- 2.34 Calculating a 32-Bit CRC
- 2.35 Calculating the SHA-256 Hash of a String
- 2.36 Calculating the Levenshtein Distance Between Two Strings
- 2.37 Encoding and Decoding Base64 Strings
- 2.38 Expanding and Compressing Tab Characters
- 2.39 Wrapping Lines of Text
- 2.40 Conclusion
-
Chapter 3. Working with Regular Expressions
- 3.1 Regular Expression Syntax
- 3.2 Compiling Regular Expressions
- 3.3 Escaping Special Characters
- 3.4 Using Anchors
- 3.5 Using Quantifiers
- 3.6 Positive and Negative Lookahead
- 3.7 Positive and Negative Lookbehind
- 3.8 Accessing Backreferences
- 3.9 Named Matches
- 3.10 Using Character Classes
- 3.11 Extended Regular Expressions
- 3.12 Matching a Newline with a Dot
- 3.13 Using Embedded Options
- 3.14 Using Embedded Subexpressions
- 3.15 A Few Sample Regular Expressions
- 3.16 Conclusion
- Chapter 4. Internationalization in Ruby
-
Chapter 5. Performing Numerical Calculations
- 5.1 Representing Numbers in Ruby
- 5.2 Basic Operations on Numbers
- 5.3 Rounding Floating Point Values
- 5.4 Comparing Floating Point Numbers
- 5.5 Formatting Numbers for Output
- 5.6 Formatting Numbers with Commas
- 5.7 Working with Very Large Integers
- 5.8 Using BigDecimal
- 5.9 Working with Rational Values
- 5.10 Matrix Manipulation
- 5.11 Working with Complex Numbers
- 5.12 Using mathn
- 5.13 Finding Prime Factorization, GCD, and LCM
- 5.14 Working with Prime Numbers
- 5.15 Implicit and Explicit Numeric Conversion
- 5.16 Coercing Numeric Values
- 5.17 Performing Bit-Level Operations on Numbers
- 5.18 Performing Base Conversions
- 5.19 Finding Cube Roots, Fourth Roots, and So On
- 5.20 Determining the Architecture’s Byte Order
- 5.21 Numerical Computation of a Definite Integral
- 5.22 Trigonometry in Degrees, Radians, and Grads
- 5.23 Finding Logarithms with Arbitrary Bases
- 5.24 Finding the Mean, Median, and Mode of a Data Set
- 5.25 Variance and Standard Deviation
- 5.26 Finding a Correlation Coefficient
- 5.27 Generating Random Numbers
- 5.28 Caching Functions with Memoization
- 5.29 Conclusion
- Chapter 6. Symbols and Ranges
-
Chapter 7. Working with Times and Dates
- 7.1 Determining the Current Time
- 7.2 Working with Specific Times (Post-Epoch)
- 7.3 Determining the Day of the Week
- 7.4 Determining the Date of Easter
- 7.5 Finding the Nth Weekday in a Month
- 7.6 Converting Between Seconds and Larger Units
- 7.7 Converting to and from the Epoch
- 7.8 Working with Leap Seconds: Don’t!
- 7.9 Finding the Day of the Year
- 7.10 Validating a Date or Time
- 7.11 Finding the Week of the Year
- 7.12 Detecting Leap Years
- 7.13 Obtaining the Time Zone
- 7.14 Working with Hours and Minutes Only
- 7.15 Comparing Time Values
- 7.16 Adding Intervals to Time Values
- 7.17 Computing the Difference in Two Time Values
- 7.18 Working with Specific Dates (Pre-Epoch)
- 7.19 Time, Date, and DateTime
- 7.20 Parsing a Date or Time String
- 7.21 Formatting and Printing Time Values
- 7.22 Time Zone Conversions
- 7.23 Determining the Number of Days in a Month
- 7.24 Dividing a Month into Weeks
- 7.25 Conclusion
-
Chapter 8. Arrays, Hashes, and Other Enumerables
-
8.1 Working with Arrays
- 8.1.1 Creating and Initializing an Array
- 8.1.2 Accessing and Assigning Array Elements
- 8.1.3 Finding an Array’s Size
- 8.1.4 Comparing Arrays
- 8.1.5 Sorting an Array
- 8.1.6 Selecting from an Array by Criteria
- 8.1.7 Using Specialized Indexing Functions
- 8.1.8 Implementing a Sparse Matrix
- 8.1.9 Using Arrays as Mathematical Sets
- 8.1.10 Randomizing an Array
- 8.1.11 Using Multidimensional Arrays
- 8.1.12 Finding Elements in One Array But Not Another
- 8.1.13 Transforming or Mapping Arrays
- 8.1.14 Removing nil Values from an Array
- 8.1.15 Removing Specific Array Elements
- 8.1.16 Concatenating and Appending onto Arrays
- 8.1.17 Using an Array as a Stack or Queue
- 8.1.18 Iterating over an Array
- 8.1.19 Interposing Delimiters to Form a String
- 8.1.20 Reversing an Array
- 8.1.21 Removing Duplicate Elements from an Array
- 8.1.22 Interleaving Arrays
- 8.1.23 Counting Frequency of Values in an Array
- 8.1.24 Inverting an Array to Form a Hash
- 8.1.25 Synchronized Sorting of Multiple Arrays
- 8.1.26 Establishing a Default Value for New Array Elements
-
8.2 Working with Hashes
- 8.2.1 Creating a New Hash
- 8.2.2 Specifying a Default Value for a Hash
- 8.2.3 Accessing and Adding Key-Value Pairs
- 8.2.4 Deleting Key-Value Pairs
- 8.2.5 Iterating Over a Hash
- 8.2.6 Inverting a Hash
- 8.2.7 Detecting Keys and Values in a Hash
- 8.2.8 Extracting Hashes into Arrays
- 8.2.9 Selecting Key-Value Pairs by Criteria
- 8.2.10 Sorting a Hash
- 8.2.11 Merging Two Hashes
- 8.2.12 Creating a Hash from an Array
- 8.2.13 Finding Difference or Intersection of Hash Keys
- 8.2.14 Using a Hash as a Sparse Matrix
- 8.2.15 Implementing a Hash with Duplicate Keys
- 8.2.16 Other Hash Operations
- 8.3 Enumerables in General
- 8.4 More on Enumerables
- 8.5 Conclusion
-
8.1 Working with Arrays
- Chapter 9. More Advanced Data Structures
-
Chapter 10. I/O and Data Storage
-
10.1 Working with Files and Directories
- 10.1.1 Opening and Closing Files
- 10.1.2 Updating a File
- 10.1.3 Appending to a File
- 10.1.4 Random Access to Files
- 10.1.5 Working with Binary Files
- 10.1.6 Locking Files
- 10.1.7 Performing Simple I/O
- 10.1.8 Performing Buffered and Unbuffered I/O
- 10.1.9 Manipulating File Ownership and Permissions
- 10.1.10 Retrieving and Setting Timestamp Information
- 10.1.11 Checking File Existence and Size
- 10.1.12 Checking Special File Characteristics
- 10.1.13 Working with Pipes
- 10.1.14 Performing Special I/O Operations
- 10.1.15 Using Nonblocking I/O
- 10.1.16 Using readpartial
- 10.1.17 Manipulating Pathnames
- 10.1.18 Using the Pathname Class
- 10.1.19 Command-Level File Manipulation
- 10.1.20 Grabbing Characters from the Keyboard
- 10.1.21 Reading an Entire File into Memory
- 10.1.22 Iterating Over a File by Lines
- 10.1.23 Iterating Over a File by Byte or Character
- 10.1.24 Treating a String As a File
- 10.1.25 Copying a Stream
- 10.1.26 Working with Character Encodings
- 10.1.27 Reading Data Embedded in a Program
- 10.1.28 Reading Program Source
- 10.1.29 Working with Temporary Files
- 10.1.30 Changing and Setting the Current Directory
- 10.1.31 Changing the Current Root
- 10.1.32 Iterating Over Directory Entries
- 10.1.33 Getting a List of Directory Entries
- 10.1.34 Creating a Chain of Directories
- 10.1.35 Deleting a Directory Recursively
- 10.1.36 Finding Files and Directories
- 10.2 Higher-Level Data Access
- 10.3 Connecting to External Data Stores
- 10.4 Conclusion
-
10.1 Working with Files and Directories
-
Chapter 11. OOP and Dynamic Features in Ruby
-
11.1 Everyday OOP Tasks
- 11.1.1 Using Multiple Constructors
- 11.1.2 Creating Instance Attributes
- 11.1.3 Using More Elaborate Constructors
- 11.1.4 Creating Class-Level Attributes and Methods
- 11.1.5 Inheriting from a Superclass
- 11.1.6 Testing Classes of Objects
- 11.1.7 Testing Equality of Objects
- 11.1.8 Controlling Access to Methods
- 11.1.9 Copying an Object
- 11.1.10 Using initialize_copy
- 11.1.11 Understanding allocate
- 11.1.12 Working with Modules
- 11.1.13 Transforming or Converting Objects
- 11.1.14 Creating Data-Only Classes (Structs)
- 11.1.15 Freezing Objects
- 11.1.16 Using tap in Method Chaining
-
11.2 More Advanced Techniques
- 11.2.1 Sending an Explicit Message to an Object
- 11.2.2 Specializing an Individual Object
- 11.2.3 Nesting Classes and Modules
- 11.2.4 Creating Parametric Classes
- 11.2.5 Storing Code as Proc Objects
- 11.2.6 Storing Code as Method Objects
- 11.2.7 Using Symbols as Blocks
- 11.2.8 How Module Inclusion Works
- 11.2.9 Detecting Default Parameters
- 11.2.10 Delegating or Forwarding
- 11.2.11 Defining Class-Level Readers and Writers
- 11.2.12 Working in Advanced Programming Disciplines
-
11.3 Working with Dynamic Features
- 11.3.1 Evaluating Code Dynamically
- 11.3.2 Retrieving a Constant by Name
- 11.3.3 Retrieving a Class by Name
- 11.3.4 Using define_method
- 11.3.5 Obtaining Lists of Defined Entities
- 11.3.6 Removing Definitions
- 11.3.7 Handling References to Nonexistent Constants
- 11.3.8 Handling Calls to Nonexistent Methods
- 11.3.9 Improved Security with taint
- 11.3.10 Defining Finalizers for Objects
- 11.4 Program Introspection
- 11.5 Conclusion
-
11.1 Everyday OOP Tasks
- Chapter 12. Graphical Interfaces for Ruby
-
Chapter 13. Threads and Concurrency
- 13.1 Creating and Manipulating Threads
-
13.2 Synchronizing Threads
- 13.2.1 Performing Simple Synchronization
- 13.2.2 Synchronizing Access with a Mutex
- 13.2.3 Using the Built-in Queue Classes
- 13.2.4 Using Condition Variables
- 13.2.5 Other Synchronization Techniques
- 13.2.6 Setting a Timeout for an Operation
- 13.2.7 Waiting for an Event
- 13.2.8 Collection Searching in Parallel
- 13.2.9 Recursive Deletion in Parallel
- 13.3 Fibers and Cooperative Multitasking
- 13.4 Conclusion
- Chapter 14. Scripting and System Administration
- Chapter 15. Ruby and Data Formats
- Chapter 16. Testing and Debugging
- Chapter 17. Packaging and Distributing Code
-
Chapter 18. Network Programming
- 18.1 Network Servers
-
18.2 Network Clients
- 18.2.1 Retrieving Truly Random Numbers from the Web
- 18.2.2 Contacting an Official Timeserver
- 18.2.3 Interacting with a POP Server
- 18.2.4 Sending Mail with SMTP
- 18.2.5 Interacting with an IMAP Server
- 18.2.6 Encoding/Decoding Attachments
- 18.2.7 Case Study: A Mail-News Gateway
- 18.2.8 Retrieving a Web Page from a URL
- 18.2.9 Using the Open-URI Library
- 18.3 Conclusion
- Chapter 19. Ruby and Web Applications
- Chapter 20. Distributed Ruby
- Chapter 21. Ruby Development Tools
- Chapter 22. The Ruby Community
- Index
- Code Snippets
Product information
- Title: The Ruby Way: Solutions and Techniques in Ruby Programming, Third Edition
- Author(s):
- Release date: March 2015
- Publisher(s): Addison-Wesley Professional
- ISBN: 9780132480352
You might also like
book
The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition
Ruby is an agile object-oriented language, borrowing some of the best features from LISP, Smalltalk, Perl, …
book
Effective Ruby: 48 Specific Ways to Write Better Ruby
If you’re an experienced Ruby programmer, will help you harness Ruby’s full power to write more …
book
Comprehensive Ruby Programming
This book will provide you with all of the tools you need to be a professional …
book
Ruby Best Practices
How do you write truly elegant code with Ruby? Ruby Best Practices is for programmers who …