1593272944 {8C789BFD} the book of ruby a hands on guide for the adventurous collingbourne 2011 07 16

402 864 0
1593272944 {8C789BFD} the book of ruby  a hands on guide for the adventurous collingbourne 2011 07 16

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

MASTER RUBY FROM THE INSIDE OUT You’ll start with the basics—types, data structures, and control flows—and progress to advanced features like blocks, mixins, metaclasses, and beyond Rather than bog you down with a lot of theory, The Book of Ruby takes a hands-on approach and focuses on making you productive from day one As you follow along, you’ll learn to: • Leverage Ruby’s succinct and flexible syntax to maximize your productivity • Write self-modifying programs using dynamic programming techniques • Create new fibers and threads for lightweight multitasking • Develop powerful web applications with the Ruby on Rails framework Each chapter includes a “Digging Deeper” section that shows you how Ruby works under the hood, so you won’t be caught off guard by its deceptively simple scoping, multithreading features, or precedence rules Whether you’re new to programming or just new to Ruby, The Book of Ruby is your guide to mastering rapid, real-world software development with this unique and elegant language ABOUT THE AUTHOR Huw Collingbourne is the Director of Technology for SapphireSteel Software, developer of the Ruby In Steel IDE With 30 years of programming experience, he has written programming columns for numerous magazines, presented features on computing for BBC Television, and currently edits the online technical journal Bitwise Magazine He has previously released two free ebooks on Ruby—The Little Book of Ruby and The Book of Ruby w w w.nostarch.com $39.95 ($45.95 CDN) This book uses a lay-flat binding that won’t snap shut FSC LOGO SHELVE IN: COMPUTERS/PROGRAMMING LANGUAGES/RUBY “ I L I E F L AT ” CO L L I N G BO U R N E T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™ ™ • Balance Ruby’s functional, imperative, and objectoriented features • Catch and recover from execution errors with robust exception handling THE BOOK OF RUBY Ruby is famous for being easy to learn, but most users only scratch the surface of what it can While other books focus on Ruby’s trendy features, The Book of ™ Ruby reveals the secret inner workings of one of the world’s most popular programming languages, teaching you to write clear, maintainable code THE BOOK OF Covers Ruby 1.8 and 1.9 ™ RUBY A H A N D S - O N G U I D E F O R T H E ADVENTUROUS HUW COLLINGBOURNE THE BOOK OF RUBY ™ THE BOOK OF RUBY ™ A H a n d s-On Guide for the Adventurous by Huw Collingbourne San Francisco THE BOOK OF RUBY Copyright © 2011 by Huw Collingbourne All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher Printed in Canada 15 14 13 12 11 123456789 ISBN-10: 1-59327-294-4 ISBN-13: 978-1-59327-294-4 Publisher: William Pollock Production Editor: Serena Yang Developmental Editor: Keith Fancher Technical Reviewer: Pat Eyler Copyeditor: Kim Wimpsett Compositors: Serena Yang and Alison Law Proofreader: Ward Webber For information on book distributors or translations, please contact No Starch Press, Inc directly: No Starch Press, Inc 38 Ringold Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com Library of Congress Cataloging-in-Publication Data Collingbourne, Huw The book of Ruby : a hands-on guide for the adventurous / Huw Collingbourne p cm Includes index ISBN-13: 978-1-59327-294-4 ISBN-10: 1-59327-294-4 Ruby (Computer program language) Object-oriented programming (Computer science) I Title QA76.73.R83C65 2011 005.1'17 dc23 2011014782 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc “The Book of” is a trademark of No Starch Press, Inc Other product and company names mentioned herein may be the trademarks of their respective owners Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The information in this book is distributed on an “As Is” basis, without warranty While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it BRIEF CONTENTS Acknowledgments xv Introduction xvii Chapter 1: Strings, Numbers, Classes, and Objects Chapter 2: Class Hierarchies, Attributes, and Class Variables 15 Chapter 3: Strings and Ranges .33 Chapter 4: Arrays and Hashes .47 Chapter 5: Loops and Iterators 67 Chapter 6: Conditional Statements 83 Chapter 7: Methods 97 Chapter 8: Passing Arguments and Returning Values .121 Chapter 9: Exception Handling 139 Chapter 10: Blocks, Procs, and Lambdas .155 Chapter 11: Symbols 181 Chapter 12: Modules and Mixins 191 Chapter 13: Files and IO .213 Chapter 14: YAML 227 Chapter 15: Marshal 239 Chapter 16: Regular Expressions 249 Chapter 17: Threads 263 Chapter 18: Debugging and Testing .283 Chapter 19: Ruby on Rails 299 Chapter 20: Dynamic Programming 325 Appendix A: Documenting Ruby with RDoc 345 Appendix B: Installing MySQL for Ruby on Rails 349 Appendix C: Further Reading .353 Appendix D: Ruby and Rails Development Software 357 Index 361 vi B ri e f C on t e n t s CONTENTS IN DETAIL A C KN O W L E D G M E N T S INTRODUCTION xv xvii What Is Ruby? xviii What Is Rails? .xviii Matters of Ruby Style .xviii How to Read This Book xix Digging Deeper xix Making Sense of the Text xix Downloading Ruby xx Getting the Source Code of the Sample Programs xxi Running Ruby Programs xxi The Ruby Library Documentation xxi STRINGS, NUMBERS, CLASSES, AND OBJECTS Getting and Putting Input Strings and Embedded Evaluation Numbers Comments Testing a Condition: if then Local and Global Variables Classes and Objects Instance Variables Retrieving Data from an Object Messages, Methods, and Polymorphism Constructors: new and initialize 10 Inspecting Objects 11 C L A S S H I E R A R C H I E S , A T T R IB U T E S , A N D CLASS VARIABLES 15 Superclasses and Subclasses 17 Passing Arguments to the Superclass 18 Accessor Methods 19 Attribute Readers and Writers 20 Calling Methods of a Superclass 23 Class Variables 23 Digging Deeper 25 Superclasses 25 The Root of All Classes 29 Constants Inside Classes 30 Partial Classes 30 STRINGS AND RANGES 33 User-Defined String Delimiters 35 Backquotes 36 String Handling 36 Concatenation 37 What About Commas? 38 String Assignment 38 Indexing into a String 39 Removing Newline Characters: chop and chomp 42 Format Strings 43 Ranges 43 Ranges of Strings 44 Iterating with a Range 44 Digging Deeper 45 Heredocs 45 String Literals 46 ARRAYS AND HASHES 47 Arrays 47 Creating Arrays 48 Multidimensional Arrays 50 Iterating over Arrays 51 Indexing into Arrays 52 Copying Arrays 53 Testing Arrays for Equality 53 Sorting Arrays 54 Comparing Values 55 Array Methods 57 Hashes 58 Creating Hashes 58 Indexing into a Hash 60 Copying a Hash 60 Hash Order 60 Sorting a Hash 61 Hash Methods 62 Digging Deeper 63 Treating Hashes as Arrays 63 Appending vs Concatenating 63 Vectors and Matrices 64 Sets 65 LOOPS AND ITERATORS 67 for Loops 67 Blocks and Block Parameters 70 Iterating upto and downto 70 viii Contents i n Detail Multiple Iterator Arguments 71 while Loops 72 while Modifiers 72 Ensuring a while Loop Executes at Least Once 73 until Loops 74 loop 75 Digging Deeper 76 The Enumerable Module 76 Custom Comparisons 77 each and yield 81 C O N D IT IO NA L ST AT EM ENT S 83 if then else 84 and, or, and not 85 Negation 86 if elsif 86 unless 88 if and unless Modifiers 88 Case Statements 89 The === Method 92 Alternative Case Syntax 92 Digging Deeper 93 Boolean Operators 93 Eccentricities of Boolean Operators 94 catch and throw 94 M E TH O D S 97 Class Methods 97 What Are Class Methods For? 98 Class Variables 99 Ruby Constructors: new or initialize? 104 Singleton Methods 105 Singleton Classes 108 Overriding Methods 110 Public, Protected, and Private Methods 111 Digging Deeper 114 Protected and Private Methods in Descendant Classes 114 Invading the Privacy of Private Methods 116 Singleton Class Methods 116 Nested Methods 118 Method Names 119 PASSING ARGUMENTS AND RETURNING VALUES 121 Summarizing Instance, Class, and Singleton, Methods 121 Returning Values 123 Contents in D etai l ix files (continued) and directories, 217 eof, 215 exist?, 217 foreach, 254, 258 handles, 215 modes, 214 opening, 214 processing using regular expressions, 258–259 reading/writing text, 215, 259 saving data to YAML, 231 size of, 220 calculating, 219 sorting by, 222 as a subclass of IO, 214 FileTest, 206 FileUtils, 218 Fixnums, 136 See also integers flunk, 296 foreach, 213 for in loops, 51 for loops, 67–70 and arrays, 71 and blocks, 180 and local variable scoping, 180 and multiple iterator arguments, 71 and ranges, 69 format strings, 43 freeze, 211, 342–343 frozen?, 211, 342 functions, 157–158 See also methods and module_function, 209 G garbage collection, 11 GC, 206 Gemfile, 319 Gems, Ruby, 300 generate, 306 getc, 216, 248 global variables, 5, 184 See also variables goto, 94 366 INDEX H Hansson, David Heinemeier, 316 hashes, 58 copying, 60 creating, 58–59 and curly brackets, 161–162 has_key?, 62 has_value?, 62 indexing into, 60 items in deleting, 62 finding position of, 62 merging, 62 methods, 62 order of elements, 60, 229 sort, 223 sorting, 61 treating as arrays, 63 hask mark (#), for comments, heredocs, 45–46 HTML, tags, 309, 311 See also Rails I id2name, 186 IDEs (integrated development environments), 283, 357–358 if else, 84 if elsif, 86 if modifiers, 88–89 if then, 4–5 if then else, 84 include, 194 See also modules include?, 76, 88 See also Enumerable index, 62 information hiding, 128 inheritance, 16 initialize, 10–11 input and output See IO (input and output) inspect, 11–13 instance_eval, 329 instance methods, 121–122 instance_variable_get, 103, 333 instance variables, 7, 23, 100 instance_variables, 197 instance_variable_set, 332 integers loops, 73 object_id, 127 testing for equality, 136 integrated development environments (IDEs), 283, 357–358 Interactive Ruby (IRB), 283–286 invert, 62 IO (input and output) class, 213 eof, 215 exceptions in, 144 foreach, 213 getc, 215, 248 gets, print, printf, 43, 221 putc, 215 puts, readlines, 214 IRB (Interactive Ruby), 283–286 IronRuby, 359 J JRuby, 359 K Kernel, 193, 206, 330 key, 62 keys, 59, 62 L lambda, 162–163 lambdas, 161 less-than sign (

Ngày đăng: 07/01/2017, 20:50

Mục lục

  • Copyright

  • Brief Contents

  • Contents in Detail

  • Acknowledgments

  • Introduction

    • What Is Ruby?

    • What Is Rails?

    • Matters of Ruby Style

    • How to Read This Book

    • Digging Deeper

    • Making Sense of the Text

    • Downloading Ruby

    • Getting the Source Code of the Sample Programs

    • Running Ruby Programs

    • The Ruby Library Documentation

    • 1: Strings, Numbers, Classes, and Objects

      • Getting and Putting Input

      • Strings and Embedded Evaluation

      • Numbers

      • Comments

      • Testing a Condition: if..then

      • Local and Global Variables

Tài liệu cùng người dùng

Tài liệu liên quan