Ruby Cookbook: Recipes for Object-Oriented Scripting

所需积分/C币:1 2018-04-19 11:11:18 6.75MB PDF
6
收藏 收藏
举报

无水印,数字版,英文原版,有目录。 Ruby Cookbook: Recipes for Object-Oriented Scripting Why spend time on coding problems that others have already solved when you could be making real progress on your Ruby project? This updated cookbook provides more than 350 recipes for solving common problems, on topics ranging f
SECOND EDITION Ruby cookbook Lucas Carlson and leonard richardson Beng. Cambridge. Farnham·Kn· Sebastopol, Tokyo OREILLY° Ruby〔 bokbok by Lucas Carlson and Leonard Richardson Copyright C 2015 Lucas Carlson and Leonard Richardson. All rights reserved Printed in the United states of america Published by O reilly Media, Inc, 1005 Gravenstein Highway North, Sebastopol, CA95472 OReilly books may be purchased for educational, business, or sales promotional use. Online editions are alsoavailableformosttitles(http://safaribooksonline.com).Formoreinformationcontactourcorporate institutionalsalesdepartment800-998-9938orcorporate@oreilly.com Editors: Brian Anderson and Allyson MacDonald Interior Designer: David Futato Production editor: Matthew hacker Cover Designer: Ellie Volckhausen Proofreader: Rachel Monaghan Illustrator: rebecca demarest Indexer: Angela Howard July 2006 First edition March 2015 Second edition Revision history for the Second edition 2015-03-10: First release Seehttp://oreilly.com/catalog/errata.csp?isbn=9781449373719forreleasedetails The O Reilly logo is a registered trademark of O Reilly Media, Inc. Ruby Cookbook, the cover image of a side-striped jackal, and related trade dress are trademarks of O Reilly media, Inc While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/ or rights 978-1-449-37371-9 For Yoscelina, my muse and inspiration for everything great I have ever accomplished For Hugh and Valentina, the most incredible miracles ever. For Tess, who sat by me the whole time Lucas carlson For sumana eonard richardson Table of contents Preface, 1. ruby 2.1 1. 1 What's Different Between Ruby 1.8 and 2.1? 1.2 YARV(Yet Another Ruby vM) Bytecode Interpreter 1. 3 Syntax Changes 11 1.4 Keyword arguments 14 1.5 Performance Enhancements 15 1.6 Refinements 16 1.7 Debugging with DTrace and TracePoint 17 1.8 Module Prepending 19 1.9 New Methods 21 1. 10 New Classes 1.11 New Standard libraries 26 1.12 What's Next? 2. Strings. 29 2.1 Building a String from Parts 33 2.2 Substituting Variables into String 35 2.3 Substituting Variables into an Existing String 37 2. 4 Reversing a String by Words or Characters 9 2.5 Representing Unprintable Characters 2.6 Converting Between Characters and values 43 2.7 Converting Between Strings and Symbols 2. 8 Processing a String One Character at a Time 2.9 Processing a String One Word at a Time 2.10 Changing the Case of a String 2.11 Managing Whitespace 50 2.12 Testing Whether an Object Is String-Like 52 2. 13 Getting the Parts of a String You Want 53 2. 14 Word-Wrapping Lines of Text 54 2.15 Generating a Succession of Strings 2.16 Matching Strings with Regular Expressions 59 2. 17 Replacing Multiple Patterns in a Single Pass 61 2.18 Validating an Email address 63 2.19 Classifying Text with a Bayesian Analyzer 66 3. Numbers 69 3. 1 Parsing a Number from a String 3. 2 Comparing Floating-Point Numbers 73 3. 3 Representing Numbers to arbitrary Precision 3.4 Representing rational numbers 79 3.5 Generating Random Numbers 3.6 Converting between Numeric Bases 82 3.7 Taking logarithms 83 3.8 Finding Mean, Median, and Mode 86 3.9 Converting Between Degrees and Radians 89 3. 10 Multiplying Matrices 90 3. 11 Solving a System of linear equations 94 3. 12 USing Complex Numbers 3. 13 Simulating a Subclass of Fixnum 99 3. 14 Doing Math with Roman Numbers 103 3. 15 Generating a Sequence of numbers 109 3. 16 Generating Prime Numbers 3. 17 Checking a Credit Card Checksum 116 4. Date and 4. 1 Finding Today's Date 122 4.2 Parsing Dates, Precisely or Fuzzily 126 4.3 Printing a Date 129 4.4 Iterating Over dates 134 4.5 Doing Date Arithmetic 135 4.6 Counting the Days Since an Arbitrary Date 138 4.7 Converting Between Time Zones 140 4.8 Checking Whether Daylight Saving Time Is in Effect 142 4.9 Converting Between Time and Date Time Objects 4.10 Finding the Day of the Week 147 4.11 Handling Commercial Dates 149 4.12 Running a Code block periodically 150 4.13 Waiting a Certain Amount of Time 152 Table of contents 4. 14 Adding a Timeout to a long-Running Operation 155 5. Arrays.......... 157 5.1 Iterating Over an Array 159 5.2 Rearranging Values Without Using Temporary variables 163 5.3 Stripping Duplicate Elements from an Array 165 5.4 Reversing an array 166 5.5 Sorting an array 167 5.6 Ignoring Case When Sorting Strings 169 5. 7 Making Sure a Sorted Array Stays Sorted 170 5.8 Summing the Items of an Array 175 5.9 Sorting an Array by Frequency of Appearance 177 5.10 Shuffling an Array 179 5.11 Getting the N smallest Items of an Array 180 5.12 Building a Hash from an Array 183 5.13 Extracting Portions of Arrays 185 5.14 Computing Set Operations on Arrays 188 5. 15 Partitioning or Classifying a Set 191 6.Hashe.197 6.1 Using Symbols as Hash Ke 200 6.2 Creating a Hash with a Default value 201 6.3 Adding Elements to a Hash 203 6.4 Removing elements from a hash 205 6.5 USing an Array or Other Modifiable Object as a Hash Key 206 6.6 Keeping Multiple Values for the Same Hash Key 209 6.7 Iterating Over a hash 210 6.8 Iterating Over a Hash in Insertion Order 213 6.9 Printing a Hash 214 6.10 Inverting a Hash 216 6. 11 Choosing Randomly from a Weighted List 217 6.12 Building a Histogram 220 6.13 Remapping the Keys and Values of a Hash 222 6.14 Extracting Portions of Hashes 223 6.15 Searching a Hash with Regular Expressions 224 7. Files and directories ,227 7. 1 Checking to See If a File exists 230 7. 2 Checking Your Access to a file 232 7.3 Changing the Permissions on a File 234 7.4 Seeing When a File Was last Used 237 7.5 Listing a Directory 239 Table of contents|ⅶi 7.6 Reading the Contents of a File 242 7.7 Writing to a File 246 7.8 Writing to a Temporary file 247 7.9 Picking a Random Line from a file 249 7.10 Comparing Two Files 250 7. 11 Performing Random Access on Read-Once Input Streams 254 7. 12 Walking a Directory Tree 256 7. 13 Locking a File 259 7.14 Backing Up to Versioned Filenames 262 7.15 Pretending a String Is a file 265 7. 16 Redirecting Standard Input or Output 268 7. 17 Processing a Binary file 270 7.18 Deleting a File 274 7.19 Truncating a File 275 7. 20 Finding the Files You Want 277 7.21 Finding and Changing the Current Working Directory 279 8. Code blocks and iteration 281 8. 1 Creating and Invoking a block 284 8.2 Writing a Method That Accepts a block 286 8.3 Binding a block argument to a variable 289 8.4 Blocks as Closures: Using Outside Variables Within a Code Block 291 8.5 Writing an Iterator Over a Data Structure 293 8.6 Changing the Way an Object Iterates 296 8.7 Writing block methods That Classify or Collect 298 8.8 Stopping an Iteratic 300 8.9 Looping Through multiple Iterables in Parallel 302 8. 10 Hiding Setup and cleanup in a block method 306 8. 11 Coupling Systems Loosely with Callbacks 308 9. Objects and classes 9.1 Managing Instance Data 316 9.2 Managing Class Data 318 9.3 Checking Class or Module Membership 321 9.4 Writing an Inherited Class 323 9.5 Overloading methods 326 9.6 Validating and Modifying Attribute values 328 9.7 Defining a Virtual Attribute 330 9. 8 Delegating Method Calls to Another Object 331 9.9 Converting and Coercing Objects to Different Types 334 9.10 Getting a Human- Readable Printout of Any object 339 9.11 Accepting or Passing a Variable Number of Arguments 341 I Table of Contents

...展开详情
试读 127P Ruby Cookbook: Recipes for Object-Oriented Scripting
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
上传资源赚钱or赚积分
最新推荐
Ruby Cookbook: Recipes for Object-Oriented Scripting 1积分/C币 立即下载
1/127
Ruby Cookbook: Recipes for Object-Oriented Scripting第1页
Ruby Cookbook: Recipes for Object-Oriented Scripting第2页
Ruby Cookbook: Recipes for Object-Oriented Scripting第3页
Ruby Cookbook: Recipes for Object-Oriented Scripting第4页
Ruby Cookbook: Recipes for Object-Oriented Scripting第5页
Ruby Cookbook: Recipes for Object-Oriented Scripting第6页
Ruby Cookbook: Recipes for Object-Oriented Scripting第7页
Ruby Cookbook: Recipes for Object-Oriented Scripting第8页
Ruby Cookbook: Recipes for Object-Oriented Scripting第9页
Ruby Cookbook: Recipes for Object-Oriented Scripting第10页
Ruby Cookbook: Recipes for Object-Oriented Scripting第11页
Ruby Cookbook: Recipes for Object-Oriented Scripting第12页
Ruby Cookbook: Recipes for Object-Oriented Scripting第13页
Ruby Cookbook: Recipes for Object-Oriented Scripting第14页
Ruby Cookbook: Recipes for Object-Oriented Scripting第15页
Ruby Cookbook: Recipes for Object-Oriented Scripting第16页
Ruby Cookbook: Recipes for Object-Oriented Scripting第17页
Ruby Cookbook: Recipes for Object-Oriented Scripting第18页
Ruby Cookbook: Recipes for Object-Oriented Scripting第19页
Ruby Cookbook: Recipes for Object-Oriented Scripting第20页

试读结束, 可继续阅读

1积分/C币 立即下载