Thông tin tài liệu
Ruby is famous for being easy to learn, but most users
only scratch the surface of what it can do. 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.
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
• Balance Ruby’s functional, imperative, and object-
oriented features
• Write self-modifying programs using dynamic
programming techniques
• Create new fibers and threads for lightweight
multitasking
MASTER
RUBY FROM THE
INSIDE OUT
MASTER
RUBY FROM THE
INSIDE OUT
• Catch and recover from execution errors with robust
exception handling
• 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.
Covers Ruby 1.8 and 1.9
SHELVE IN :
COMPUTERS/PROGRAMMING
LANGUAGES/RUBY
$39.95 ($45.95 CDN)
www.nostarch.com
THE FINEST IN GEEK ENTERTAINMENT
™
“I LIE FLAT.”
This book uses a lay-flat binding that won’t snap shut.
FSC LOGO
HUW COLLINGBOURNE
THE BOOK OF
RUBY
THE BOOK OF
RUBY
A HANDS-ON GUIDE FOR THE ADVENTUROUS
™
™
THE BOOK OF RUBY
THE BOOK OF RUBY
COLLINGBOURNE
www.it-ebooks.info
www.it-ebooks.info
THE BOOK OF
™
RUBY
www.it-ebooks.info
www.it-ebooks.info
THE BOOK
OF
™
RUBY
A Hands-On Guide for
the Adventurous
by Huw Collingbourne
San Francisco
www.it-ebooks.info
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 1 2 3 4 5 6 7 8 9
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
1. Ruby (Computer program language) 2. 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.
www.it-ebooks.info
BRIEF CONTENTS
Acknowledgments xv
Introduction xvii
Chapter 1: Strings, Numbers, Classes, and Objects 1
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
www.it-ebooks.info
vi Brief Contents
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
www.it-ebooks.info
CONTENTS IN DETAIL
ACKNOWLEDGMENTS xv
INTRODUCTION 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
1
STRINGS, NUMBERS, CLASSES, AND OBJECTS 1
Getting and Putting Input 2
Strings and Embedded Evaluation 2
Numbers 3
Comments 4
Testing a Condition: if then 4
Local and Global Variables 5
Classes and Objects 6
Instance Variables 7
Retrieving Data from an Object 7
Messages, Methods, and Polymorphism 9
Constructors: new and initialize 10
Inspecting Objects 11
2
CLASS HIERARCHIES, ATTRIBUTES, AND
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
www.it-ebooks.info
viii Contents in Detail
3
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
4
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
5
LOOPS AND ITERATORS 67
for Loops 67
Blocks and Block Parameters 70
Iterating upto and downto 70
www.it-ebooks.info
[...]... example, this is the command you would enter in order to run the helloworld.rb program: ruby helloworld.rb If you use a Ruby IDE, you may be able to load the Ruby programs into the IDE and run them using the integrated tools of that IDE The Ruby Library Documentation The Book of Ruby covers many of the classes and methods in the standard Ruby library—but by no means all of them! At some stage, therefore,... hazards along the way www.it-ebooks.info The Book of Ruby describes versions 1.8.x and 1.9.x of the Ruby language In most respects, Ruby 1.8 and 1.9 are very similar, and most programs written for one version will run unmodified in the other There are important exceptions to this rule, however, and these are noted in the text Ruby 1.9 may be regarded as a stepping stone toward Ruby 2.0 At the time of writing,... persuade you of the merits of the Ruby language Instead, I’ll take the somewhat unconventional step of starting with a warning: Many people are attracted to Ruby by its simple syntax and its ease of use They are wrong Ruby s syntax may look simple at first sight, but the more you get to know the language, the more you will realize that it is, on the contrary, extremely complex The plain fact of the matter... installing Ruby using the Ruby Installer, available at http://www.rubyinstaller.org/ There are also several alternative implementations of Ruby, the most established of which is JRuby You can find information on where to download these implementations in Appendix D xx I n t r od u ct i on www.it-ebooks.info Getting the Source Code of the Sample Programs All the programs in every chapter in this book are... that Ruby has a number of pitfalls just waiting for unwary programmers to drop into In this book, it is my aim to guide you safely over the pitfalls and lead you through the choppy waters of Ruby s syntax and class libraries In the process, I’ll be exploring both the smooth, well-paved highways and the gnarlier, bumpy little byways of Ruby By the end of the journey, you should be able to use Ruby safely... representation of a Treasure object The method name, to_s, is not arbitrary the same method name is used throughout the standard Ruby object hierarchy In fact, the to_s method is defined for the Object class itself, which is the ultimate ancestor of all other classes in Ruby (with the exception of the BasicObject class, which you’ll look at more closely in the next chapter) By redefining the to_s method,... aspects of Ruby (including a few of those gnarly byways I mentioned a moment ago) in greater depth In many cases, you could skip the “Digging Deeper” sections and still learn all the Ruby you will ever need On the other hand, it is in these sections that you will often get closest to the inner workings of Ruby, so if you skip them, you are going to miss out on some pretty interesting stuff Making Sense of. .. instance variables, and you can set the values of all of them with the single initialize method rather than with many separate “set” methods; second, if the variables are all automatically initialized at the time of object creation, you will never end up with an “empty” variable (like the “nil” value returned when you tried to display the name of someotherdog in the previous program) Finally, I have... stuff Making Sense of the Text In The Book of Ruby, Ruby source code is written like this: def saysomething puts( "Hello" ) end Often the code will be annotated with comments Ruby comments are any text following a hash mark (#) on a single line The comments are ignored by the Ruby interpreter When I want to draw attention to some I n t r o d u ct i on www.it-ebooks.info xix output that Ruby displays or... http://www.nostarch.com/boruby.htm When you unzip the programs, you will find that they are grouped into a set of directories—one for each chapter Running Ruby Programs It is often useful to keep a command window open in the source directory containing your Ruby program files Assuming that the Ruby interpreter is correctly pathed on your system, you will then be able to run programs by entering ruby programname . OF RUBY THE BOOK OF RUBY A HANDS-ON GUIDE FOR THE ADVENTUROUS ™ ™ THE BOOK OF RUBY THE BOOK OF RUBY COLLINGBOURNE www.it-ebooks.info www.it-ebooks.info THE BOOK OF ™ RUBY www.it-ebooks.info www.it-ebooks.info THE. 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. Covers Ruby 1.8 and 1.9 SHELVE. Ruby is famous for being easy to learn, but most users only scratch the surface of what it can do. While other books focus on Ruby s trendy features, The Book of ™ Ruby reveals the secret
Ngày đăng: 31/03/2014, 17:20
Xem thêm: The Book of Ruby pdf, The Book of Ruby pdf