Thông tin tài liệu
www.it-ebooks.info
Learning Ruby
www.it-ebooks.info
Other resources from O’Reilly
Related titles
Ruby Cookbook
™
Ruby in a Nutshell
Ruby on Rails: Up and
Running
Ajax on Rails
Rails Cookbook
™
oreilly.com
oreilly.com is more than a complete catalog of O’Reilly books.
You’ll also find links to news, events, articles, weblogs, sample
chapters, and code examples.
oreillynet.com is the essential portal for developers interested in
open and emerging technologies, including new platforms, pro-
gramming languages, and operating systems.
Conferences
O’Reilly brings diverse innovators together to nurture the ideas
that spark revolutionary industries. We specialize in document-
ing the latest tools and systems, translating the innovator’s
knowledge into useful skills for those in the trenches. Visit
conferences.oreilly.com for our upcoming events.
Safari Bookshelf (safari.oreilly.com) is the premier online refer-
ence library for programmers and IT professionals. Conduct
searches across more than 1,000 books. Subscribers can zero in
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today for free.
www.it-ebooks.info
Learning Ruby
Michael Fitzgerald
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Paris
•
Sebastopol
•
Taipei
•
Tokyo
www.it-ebooks.info
Learning Ruby
by Michael Fitzgerald
Copyright © 2007 Michael Fitzgerald. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (safari.oreilly.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor:
Simon St.Laurent
Production Editor:
Lydia Onofrei
Proofreader:
Lydia Onofrei
Indexer:
John Bickelhaupt
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Illustrators:
Robert Romano and Jessamyn Read
Printing History:
May 2007: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Learning Ruby, the image of a giraffe, and related trade dress are trademarks of
O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information
contained herein.
This book uses RepKover
™
, a durable and flexible lay-flat binding.
ISBN-10: 0-596-52986-4
ISBN-13: 978-0-596-52986-4
[M]
www.it-ebooks.info
Robert Wayne Darrah
1950–2006
Till we meet again
www.it-ebooks.info
www.it-ebooks.info
vii
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xi
1. Ruby Basics
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
Hello, Matz 2
Interactive Ruby 12
Resources 13
Installing Ruby 15
Permission Denied 20
Associating File Types on Windows 21
Review Questions 22
2. A Quick Tour of Ruby
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
Ruby Is Object-Oriented 23
Ruby’s Reserved Words 26
Comments 27
Variables 28
Strings 30
Numbers and Operators 33
Conditional Statements 34
Arrays and Hashes 34
Methods 35
Blocks 40
Symbols 43
Exception Handling 44
Ruby Documentation 45
Review Questions 45
www.it-ebooks.info
viii | Table of Contents
3. Conditional Love
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
The if Statement 47
The case Statement 51
The while Loop 52
The loop Method 55
The for loop 56
Execution Before or After a Program 59
Review Questions 59
4. Strings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
60
Creating Strings 60
Concatenating Strings 63
Accessing Strings 63
Comparing Strings 65
Manipulating Strings 66
Case Conversion 70
Managing Whitespace, etc. 71
Incrementing Strings 73
Converting Strings 74
Regular Expressions 74
1.9 and Beyond 78
Review Questions 78
5. Math
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
Class Hierarchy and Included Modules 80
Converting Numbers 81
Basic Math Operations 81
Ranges 85
Inquiring About Numbers 86
More Math Methods 88
Math Functions 88
Rational Numbers 90
Prime Numbers 91
Review Questions 92
6. Arrays
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
93
Creating Arrays 94
Accessing Elements 97
Concatenation 99
www.it-ebooks.info
Table of Contents | ix
Set Operations 99
Unique Elements 100
Blow Your Stack 100
Comparing Arrays 101
Changing Elements 101
Deleting Elements 103
Arrays and Blocks 103
Sorting Things and About Face 104
Multidimensional Arrays 104
1.9 and Beyond 105
Other Array Methods 105
Review Questions 105
7. Hashes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
107
Creating Hashes 107
Accessing Hashes 108
Iterating over Hashes 109
Changing Hashes 110
Converting Hashes to Other Classes 113
1.9 and Beyond 113
Other Hash Methods 114
Review Questions 114
8. Working with Files
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
115
Directories 115
Creating a New File 117
Opening an Existing File 117
Deleting and Renaming Files 120
File Inquiries 120
Changing File Modes and Owner 121
The IO Class 123
Review Questions 124
9. Classes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
125
Defining the Class 126
Instance Variables 127
Accessors 129
Class Variables 130
Class Methods 131
www.it-ebooks.info
[...]... of open source Ruby projects Some of the more popular projects include Mongrel, a fast HTTP server (http://rubyforge.org/projects/mongrel), RubyGems (http://rubyforge.org/ projects/rubygems), a dead-simple tool for installing Ruby packages, and Instant Rails (http://rubyforge.org/projects/instantrails), a single-step Windows installer that includes Ruby, Rails, Apache, and MySQL The Ruby Application... Now test to make sure Ruby is in place: $ ruby -v 10 You should get this happy reply: $ ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.9.0] Alrighty then You are ready to roll with Ruby on Mac OS X Installing Ruby on Windows with the One-Click Installer It’s easy to install Ruby on Windows with the One-Click Installer, available on RubyForge at http://rubyforge.org/projects/rubyinstaller Here are... How This Book Is Organized Learning Ruby is organized into 11 chapters A brief synopsis of each follows: Chapter 1, Ruby Basics Introduces many Ruby basics, such as where to get Ruby, how to install it, and how to run a large cross-section of programs to enable you to start using Ruby immediately xii | Preface www.it-ebooks.info Chapter 2, A Quick Tour of Ruby Gallops over the Ruby terrain at a brisk... password: $ sudo make install 16 | Chapter 1: Ruby Basics www.it-ebooks.info The steps to install Ruby are very similar: 1 While in /usr/local/src, grab the archive for the latest version of Ruby (1.8.6 at this writing): $ curl -O ftp://ftp .ruby- lang.org/pub /ruby/ ruby-1.8.6.tar.gz 2 Extract the archive: $ tar xzvf ruby- 1.8.6.tar.gz 3 Change directories: $ cd ruby- 1.8.6 4 Run configure (enabling POSIX... other good stuff Ruby Central, Inc (http://www.rubycentral.org) runs the annual International Ruby Conference (http://www.rubycentral.org/conference) It usually gets sold out way early, so plan accordingly Aside from the documentation page on ruby- lang.org (http://www .ruby- lang.org/en/ documentation), http://www .ruby- doc.org is a great place to hunt down information on all things Ruby RDoc is a tool... sure to enable RubyGems when asked, as it is installed by default, and you’ll no doubt want use it later 3 Select a destination folder (such as C: \Ruby or C:\“Program Files” \Ruby) If you try to install over an older version of Ruby, you’ll be asked to uninstall the old version first 4 Add the new Ruby bin directory to your path; for example, if your Ruby directory is C: \Ruby, add C: \Ruby\ bin to your... if you get a reply like this one (good news if you do): /usr/local/bin /ruby Or just type a command to check the version of Ruby (this works on Unix/Linux and Windows): $ ruby -v or: $ ruby version If Ruby is installed, you should get an answer that looks like this: ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.9.0] If Ruby is not installed on your box, and you’re a little nervous about figuring... http://www.apple.com/downloads/dashboard/developer/rubyrdocwidget.html Figure 1-4 RDoc widget on Dashboard Ruby- Talk is the most popular general Ruby mail list To sign up (easily), go to http:// www .ruby- lang.org/en/community/mailing-lists You’ll also see several other lists at this site For a more complete list of mail groups, including lists in languages besides English, see http://www .ruby- forum.com RubyForge (http://rubyforge.org)... Installing Ruby on Windows with Binaries Installing Ruby using binaries (precompiled executables) is just as easy as using the One-Click Installer I think it is, anyway Here are the steps I suggest: 1 Decide where you want to install the Ruby files—for example, C: \Ruby or C:\ “Program Files” \Ruby 2 Download the stable binary ZIP archive for the latest release of Ruby (1.8.6 at this writing) Go to the Ruby. .. writing) Go to the Ruby download page at http://www .ruby- lang.org/en/ downloads, and find the Ruby on Windows” section, then click the link Ruby 1.8.6 Binary Or you can just point to ftp://ftp .ruby- lang.org/pub /ruby/ binaries/ mswin32 /ruby- 1.8.6-i386-mswin32.zip (or latest version) in a browser This will download the file archive 3 Open the archive (ruby- 1.8.6-i386-mswin32.zip or later) with Windows . www.it-ebooks.info
Learning Ruby
www.it-ebooks.info
Other resources from O’Reilly
Related titles
Ruby Cookbook
™
Ruby in a Nutshell
Ruby on Rails: Up and
Running
Ajax. do):
/usr/local/bin /ruby
Or just type a command to check the version of Ruby (this works on Unix/Linux and
Windows):
$ ruby -v
or:
$ ruby version
If Ruby is installed,
Ngày đăng: 17/02/2014, 23:20
Xem thêm: Tài liệu Learning Ruby ppt, Tài liệu Learning Ruby ppt