Ruby Way, The
Solutions and Techniques in Ruby Programming
Häftad, Engelska, 2015
Av Hal Fulton, André Arko
869 kr
Produktinformation
- Utgivningsdatum2015-03-12
- Mått172 x 226 x 44 mm
- Vikt1 280 g
- FormatHäftad
- SpråkEngelska
- SerieAddison-Wesley Professional Ruby Series
- Upplaga3
- FörlagPearson Education (US)
- ISBN9780321714633
Tillhör följande kategorier
Hal Fulton first began using Ruby in 1999. In 2001, he started work on The Ruby Way, which was the second Ruby book published in English. Fulton was an attendee at the very first Ruby conference in 2001 and has presented at numerous other Ruby conferences on three continents, including the first European Ruby Conference in 2003. He holds two degrees in computer science from the University of Mississippi and taught computer science for four years. He has worked for more than 25 years with various forms of UNIX and Linux. He is now at Simpli.fi in Fort Worth, Texas, where he works primarily in Ruby.André Arko first encountered Ruby as a student in 2004, and reading the first edition of this book helped him decide to pursue a career as a Ruby programmer. He is team lead of Bundler, the Ruby dependency manager, and has created or contributes to dozens of other open source projects. He works at Cloud City Development as a consultant providing team training and expertise on Ruby and Rails as well as developing web applications.André enjoys sharing hard-won knowledge and experience with other developers, and has spoken at over a dozen Ruby conferences on four continents. He is a regular volunteer at RailsBridge and RailsGirls programming outreach events, and works to increase diversity and inclusiveness in both the Ruby community and technology as a field. He lives in San Francisco, California.
- Foreword xxivAcknowledgments xxviiiAbout the Authors xxxiiIntroduction xxxiii1 Ruby in Review 11.1 An Introduction to Object Orientation 21.1.1 What Is an Object? 21.1.2 Inheritance 41.1.3 Polymorphism 61.1.4 A Few More Terms 71.2 Basic Ruby Syntax and Semantics 81.2.1 Keywords and Identifiers 91.2.2 Comments and Embedded Documentation 101.2.3 Constants, Variables, and Types 111.2.4 Operators and Precedence 131.2.5 A Sample Program 141.2.6 Looping and Branching 171.2.7 Exceptions 221.3 OOP in Ruby 251.3.1 Objects 261.3.2 Built-in Classes 261.3.3 Modules and Mixins 281.3.4 Creating Classes 291.3.5 Methods and Attributes 341.4 Dynamic Aspects of Ruby 361.4.1 Coding at Runtime 361.4.2 Reflection 381.4.3 Missing Methods 401.4.4 Garbage Collection 401.5 Training Your Intuition: Things to Remember 411.5.1 Syntax Issues 411.5.2 Perspectives in Programming 441.5.3 Ruby’s case Statement 471.5.4 Rubyisms and Idioms 501.5.5 Expression Orientation and Other Miscellaneous Issues 571.6 Ruby Jargon and Slang 591.7 Conclusion 622 Working with Strings 632.1 Representing Ordinary Strings 642.2 Representing Strings with Alternate Notations 652.3 Using Here-Documents 652.4 Finding the Length of a String 672.5 Processing a Line at a Time 682.6 Processing a Character or Byte at a Time 682.7 Performing Specialized String Comparisons 692.8 Tokenizing a String 712.9 Formatting a String 732.10 Using Strings as IO Objects 742.11 Controlling Uppercase and Lowercase 742.12 Accessing and Assigning Substrings 752.13 Substituting in Strings 782.14 Searching a String 792.15 Converting Between Characters and ASCII Codes 802.16 Implicit and Explicit Conversion 802.17 Appending an Item onto a String 832.18 Removing Trailing Newlines and Other Characters 832.19 Trimming Whitespace from a String 842.20 Repeating Strings 852.21 Embedding Expressions within Strings 852.22 Delayed Interpolation of Strings 862.23 Parsing Comma-Separated Data 862.24 Converting Strings to Numbers (Decimal and Otherwise) 872.25 Encoding and Decoding rot13 Text 892.26 Encrypting Strings 902.27 Compressing Strings 912.28 Counting Characters in Strings 922.29 Reversing a String 922.30 Removing Duplicate Characters 932.31 Removing Specific Characters 932.32 Printing Special Characters 932.33 Generating Successive Strings 942.34 Calculating a 32-Bit CRC 942.35 Calculating the SHA-256 Hash of a String 952.36 Calculating the Levenshtein Distance Between Two Strings 962.37 Encoding and Decoding Base64 Strings 982.38 Expanding and Compressing Tab Characters 982.39 Wrapping Lines of Text 992.40 Conclusion 1003 Working with Regular Expressions 1013.1 Regular Expression Syntax 1023.2 Compiling Regular Expressions 1043.3 Escaping Special Characters 1053.4 Using Anchors 1053.5 Using Quantifiers 1063.6 Positive and Negative Lookahead 1093.7 Positive and Negative Lookbehind 1103.8 Accessing Backreferences 1113.9 Named Matches 1143.10 Using Character Classes 1163.11 Extended Regular Expressions 1183.12 Matching a Newline with a Dot 1193.13 Using Embedded Options 1193.14 Using Embedded Subexpressions 1203.14.1 Recursion in Regular Expressions 1213.15 A Few Sample Regular Expressions 1223.15.1 Matching an IP Address 1223.15.2 Matching a Keyword-Value Pair 1233.15.3 Matching Roman Numerals 1243.15.4 Matching Numeric Constants 1253.15.5 Matching a Date/Time String 1253.15.6 Detecting Doubled Words in Text 1263.15.7 Matching All-Caps Words 1273.15.8 Matching Version Numbers 1273.15.9 A Few Other Patterns 1273.16 Conclusion 1284 Internationalization in Ruby 1294.1 Background and Terminology 1314.2 Working with Character Encodings 1354.2.1 Normalization 1364.2.2 Encoding Conversions 1394.2.3 Transliteration 1414.2.4 Collation 1414.3 Translations 1444.3.1 Defaults 1464.3.2 Namespaces 1474.3.3 Interpolation 1484.3.4 Pluralization 1494.4 Localized Formatting 1514.4.1 Dates and Times 1514.4.2 Numbers 1524.4.3 Currencies 1534.5 Conclusion 1535 Performing Numerical Calculations 1555.1 Representing Numbers in Ruby 1565.2 Basic Operations on Numbers 1575.3 Rounding Floating Point Values 1585.4 Comparing Floating Point Numbers 1605.5 Formatting Numbers for Output 1625.6 Formatting Numbers with Commas 1625.7 Working with Very Large Integers 1635.8 Using BigDecimal 1635.9 Working with Rational Values 1665.10 Matrix Manipulation 1675.11 Working with Complex Numbers 1715.12 Using mathn 1725.13 Finding Prime Factorization, GCD, and LCM 1735.14 Working with Prime Numbers 1745.15 Implicit and Explicit Numeric Conversion 1755.16 Coercing Numeric Values 1765.17 Performing Bit-Level Operations on Numbers 1775.18 Performing Base Conversions 1795.19 Finding Cube Roots, Fourth Roots, and So On 1805.20 Determining the Architecture’s Byte Order 1815.21 Numerical Computation of a Definite Integral 1825.22 Trigonometry in Degrees, Radians, and Grads 1835.23 Finding Logarithms with Arbitrary Bases 1845.24 Finding the Mean, Median, and Mode of a Data Set 1855.25 Variance and Standard Deviation 1875.26 Finding a Correlation Coefficient 1875.27 Generating Random Numbers 1895.28 Caching Functions with Memoization 1905.29 Conclusion 1916 Symbols and Ranges 1936.1 Symbols 1936.1.1 Symbols as Enumerations 1956.1.2 Symbols as Metavalues 1966.1.3 Symbols, Variables, and Methods 1976.1.4 Converting to/from Symbols 1976.2 Ranges 1996.2.1 Open and Closed Ranges 1996.2.2 Finding Endpoints 2006.2.3 Iterating Over Ranges 2006.2.4 Testing Range Membership 2016.2.5 Converting to Arrays 2026.2.6 Backward Ranges 2026.2.7 The Flip-Flop Operator 2036.2.8 Custom Ranges 2066.3 Conclusion 2097 Working with Times and Dates 2117.1 Determining the Current Time 2127.2 Working with Specific Times (Post-Epoch) 2127.3 Determining the Day of the Week 2147.4 Determining the Date of Easter 2157.5 Finding the Nth Weekday in a Month 2157.6 Converting Between Seconds and Larger Units 2177.7 Converting to and from the Epoch 2177.8 Working with Leap Seconds: Don’t! 2187.9 Finding the Day of the Year 2197.10 Validating a Date or Time 2197.11 Finding the Week of the Year 2207.12 Detecting Leap Years 2217.13 Obtaining the Time Zone 2227.14 Working with Hours and Minutes Only 2227.15 Comparing Time Values 2237.16 Adding Intervals to Time Values 2237.17 Computing the Difference in Two Time Values 2247.18 Working with Specific Dates (Pre-Epoch) 2247.19 Time, Date, and DateTime 2257.20 Parsing a Date or Time String 2257.21 Formatting and Printing Time Values 2267.22 Time Zone Conversions 2277.23 Determining the Number of Days in a Month 2287.24 Dividing a Month into Weeks 2297.25 Conclusion 2308 Arrays, Hashes, and Other Enumerables 2318.1 Working with Arrays 2328.1.1 Creating and Initializing an Array 2328.1.2 Accessing and Assigning Array Elements 2338.1.3 Finding an Array’s Size 2358.1.4 Comparing Arrays 2358.1.5 Sorting an Array 2378.1.6 Selecting from an Array by Criteria 2408.1.7 Using Specialized Indexing Functions 2428.1.8 Implementing a Sparse Matrix 2448.1.9 Using Arrays as Mathematical Sets 2448.1.10 Randomizing an Array 2488.1.11 Using Multidimensional Arrays 2498.1.12 Finding Elements in One Array But Not Another 2508.1.13 Transforming or Mapping Arrays 2508.1.14 Removing nil Values from an Array 2518.1.15 Removing Specific Array Elements 2518.1.16 Concatenating and Appending onto Arrays 2538.1.17 Using an Array as a Stack or Queue 2548.1.18 Iterating over an Array 2548.1.19 Interposing Delimiters to Form a String 2558.1.20 Reversing an Array 2568.1.21 Removing Duplicate Elements from an Array 2568.1.22 Interleaving Arrays 2568.1.23 Counting Frequency of Values in an Array 2578.1.24 Inverting an Array to Form a Hash 2578.1.25 Synchronized Sorting of Multiple Arrays 2588.1.26 Establishing a Default Value for New Array Elements 2598.2 Working with Hashes 2608.2.1 Creating a New Hash 2608.2.2 Specifying a Default Value for a Hash 2618.2.3 Accessing and Adding Key-Value Pairs 2628.2.4 Deleting Key-Value Pairs 2648.2.5 Iterating Over a Hash 2648.2.6 Inverting a Hash 2658.2.7 Detecting Keys and Values in a Hash 2658.2.8 Extracting Hashes into Arrays 2668.2.9 Selecting Key-Value Pairs by Criteria 2668.2.10 Sorting a Hash 2678.2.11 Merging Two Hashes 2688.2.12 Creating a Hash from an Array 2688.2.13 Finding Difference or Intersection of Hash Keys 2688.2.14 Using a Hash as a Sparse Matrix 2698.2.15 Implementing a Hash with Duplicate Keys 2708.2.16 Other Hash Operations 2738.3 Enumerables in General 2738.3.1 The inject Method 2748.3.2 Using Quantifiers 2758.3.3 The partition Method 2768.3.4 Iterating by Groups 2778.3.5 Converting to Arrays or Sets 2788.3.6 Using Enumerator Objects 2788.4 More on Enumerables 2808.4.1 Searching and Selecting 2808.4.2 Counting and Comparing 2818.4.3 Iterating 2828.4.4 Extracting and Converting 2838.4.5 Lazy Enumerators 2848.5 Conclusion 2859 More Advanced Data Structures 2879.1 Working with Sets 2889.1.1 Simple Set Operations 2889.1.2 More Advanced Set Operations 2909.2 Working with Stacks and Queues 2919.2.1 Implementing a Stricter Stack 2939.2.2 Detecting Unbalanced Punctuation in Expressions 2949.2.3 Understanding Stacks and Recursion 2959.2.4 Implementing a Stricter Queue 2979.3 Working with Trees 2989.3.1 Implementing a Binary Tree 2989.3.2 Sorting Using a Binary Tree 3009.3.3 Using a Binary Tree as a Lookup Table 3029.3.4 Converting a Tree to a String or Array 3039.4 Working with Graphs 3049.4.1 Implementing a Graph as an Adjacency Matrix 3049.4.2 Determining Whether a Graph Is Fully Connected 3079.4.3 Determining Whether a Graph Has an Euler Circuit 3089.4.4 Determining Whether a Graph Has an Euler Path 3099.4.5 Graph Tools in Ruby 3109.5 Conclusion 31010 I/O and Data Storage 31110.1 Working with Files and Directories 31310.1.1 Opening and Closing Files 31310.1.2 Updating a File 31410.1.3 Appending to a File 31510.1.4 Random Access to Files 31510.1.5 Working with Binary Files 31610.1.6 Locking Files 31810.1.7 Performing Simple I/O 31810.1.8 Performing Buffered and Unbuffered I/O 32010.1.9 Manipulating File Ownership and Permissions 32110.1.10 Retrieving and Setting Timestamp Information 32310.1.11 Checking File Existence and Size 32510.1.12 Checking Special File Characteristics 32610.1.13 Working with Pipes 32810.1.14 Performing Special I/O Operations 32910.1.15 Using Nonblocking I/O 33010.1.16 Using readpartial 33110.1.17 Manipulating Pathnames 33110.1.18 Using the Pathname Class 33310.1.19 Command-Level File Manipulation 33410.1.20 Grabbing Characters from the Keyboard 33610.1.21 Reading an Entire File into Memory 33610.1.22 Iterating Over a File by Lines 33710.1.23 Iterating Over a File by Byte or Character 33710.1.24 Treating a String As a File 33810.1.25 Copying a Stream 33910.1.26 Working with Character Encodings 33910.1.27 Reading Data Embedded in a Program 33910.1.28 Reading Program Source 34010.1.29 Working with Temporary Files 34010.1.30 Changing and Setting the Current Directory 34110.1.31 Changing the Current Root 34210.1.32 Iterating Over Directory Entries 34210.1.33 Getting a List of Directory Entries 34210.1.34 Creating a Chain of Directories 34210.1.35 Deleting a Directory Recursively 34310.1.36 Finding Files and Directories 34310.2 Higher-Level Data Access 34410.2.1 Simple Marshaling 34510.2.2 “Deep Copying” with Marshal 34610.2.3 More Complex Marshaling 34610.2.4 Marshaling with YAML 34710.2.5 Persisting Data with JSON 34910.2.6 Working with CSV Data 35010.2.7 SQLite3 for SQL Data Storage 35210.3 Connecting to External Data Stores 35310.3.1 Connecting to MySQL Databases 35410.3.2 Connecting to PostgreSQL Databases 35610.3.3 Object-Relational Mappers (ORMs) 35810.3.4 Connecting to Redis Data Stores 35910.4 Conclusion 36011 OOP and Dynamic Features in Ruby 36111.1 Everyday OOP Tasks 36211.1.1 Using Multiple Constructors 36211.1.2 Creating Instance Attributes 36411.1.3 Using More Elaborate Constructors 36611.1.4 Creating Class-Level Attributes and Methods 36811.1.5 Inheriting from a Superclass 37211.1.6 Testing Classes of Objects 37411.1.7 Testing Equality of Objects 37711.1.8 Controlling Access to Methods 37811.1.9 Copying an Object 38111.1.10 Using initialize_copy 38311.1.11 Understanding allocate 38411.1.12 Working with Modules 38411.1.13 Transforming or Converting Objects 38811.1.14 Creating Data-Only Classes (Structs) 39011.1.15 Freezing Objects 39111.1.16 Using tap in Method Chaining 39311.2 More Advanced Techniques 39411.2.1 Sending an Explicit Message to an Object 39411.2.2 Specializing an Individual Object 39611.2.3 Nesting Classes and Modules 39911.2.4 Creating Parametric Classes 40011.2.5 Storing Code as Proc Objects 40311.2.6 Storing Code as Method Objects 40511.2.7 Using Symbols as Blocks 40611.2.8 How Module Inclusion Works 40611.2.9 Detecting Default Parameters 40911.2.10 Delegating or Forwarding 40911.2.11 Defining Class-Level Readers and Writers 41211.2.12 Working in Advanced Programming Disciplines 41411.3 Working with Dynamic Features 41611.3.1 Evaluating Code Dynamically 41611.3.2 Retrieving a Constant by Name 41811.3.3 Retrieving a Class by Name 41811.3.4 Using define_method 41911.3.5 Obtaining Lists of Defined Entities 42311.3.6 Removing Definitions 42511.3.7 Handling References to Nonexistent Constants 42711.3.8 Handling Calls to Nonexistent Methods 42911.3.9 Improved Security with taint 43011.3.10 Defining Finalizers for Objects 43211.4 Program Introspection 43311.4.1 Traversing the Object Space 43411.4.2 Examining the Call Stack 43511.4.3 Tracking Changes to a Class or Object Definition 43511.4.4 Monitoring Program Execution 43911.5 Conclusion 44112 Graphical Interfaces for Ruby 44312.1 Shoes 4 44412.1.1 Starting Out with Shoes 44412.1.2 An Interactive Button 44512.1.3 Text and Input 44612.1.4 Layout 44812.1.5 Images and Shapes 45012.1.6 Events 45012.1.7 Other Notes 45112.2 Ruby/Tk 45212.2.1 Overview 45212.2.2 A Simple Windowed Application 45312.2.3 Working with Buttons 45512.2.4 Working with Text Fields 45912.2.5 Working with Other Widgets 46312.2.6 Other Notes 46712.3 Ruby/GTK3 46712.3.1 Overview 46712.3.2 A Simple Windowed Application 46812.3.3 Working with B
Praise for The Ruby Way, Third Edition “Sticking to its tried and tested formula of cutting right to the techniques the modern day Rubyist needs to know, the latest edition of The Ruby Way keeps its strong reputation going for the latest generation of the Ruby language.” Peter CooperEditor of Ruby Weekly “The authors’ excellent work and meticulous attention to detail continues in this latest update; this book remains an outstanding reference for the beginning Ruby programmer– as well as the seasoned developer who needs a quick refresh on Ruby. Highly recommended for anyone interested in Ruby programming.” Kelvin Meeks Enterprise Architect Praise for Previous Editions of The Ruby Way “Among other things, this book excels at explaining metaprogramming, one of the most interesting aspects of Ruby. Many of the early ideas for Rails were inspired by the first edition, especially what is now Chapter 11. It puts you on a rollercoaster ride between ‘How could I use this?’ and ‘This is so cool!’ Once you get on that rollercoaster, there’s no turning back.” David Heinemeier Hansson Creator of Ruby on Rails, Founder at Basecamp “The appearance of the second edition of this classic book is an exciting event for Rubyists–and for lovers of superb technical writing in general. Hal Fulton brings a lively erudition and an engaging, lucid style to bear on a thorough and meticulously exact exposition of Ruby. You palpably feel the presence of a teacher who knows a tremendous amount and really wants to help you know it too.” David Alan Black Author of The Well-Grounded Rubyist “This is an excellent resource for gaining insight into how and why Ruby works. Assomeone who has worked with Ruby for several years, I still found it full of new tricks and techniques. It’s accessible both as a straight read and as a reference that one can dip into and learn something new.” Chet Hendrickson Agile software pioneer “Ruby’s a wonderful language–but sometimes you just want to get something done. Hal’s book gives you the solution and teaches a good bit about why that solution is good Ruby.” Martin Fowler Chief Scientist, ThoughtWorks Author of Patterns of Enterprise Application Architecture