Programming Ruby 1 9 2 0 The Pragmatic Programmers Guide Fourth Edition Dave Thomas pdf download
Programming Ruby 1 9 2 0 The Pragmatic Programmers Guide Fourth Edition Dave Thomas pdf download
https://ebookfinal.com/download/programming-ruby-1-9-2-0-the-
pragmatic-programmers-guide-fourth-edition-dave-thomas/
https://ebookfinal.com/download/agile-web-development-with-rails-a-
pragmatic-guide-1st-edition-dave-thomas/
https://ebookfinal.com/download/fxruby-create-lean-and-mean-guis-with-
ruby-pragmatic-programmers-1st-edition-lyle-johnson/
https://ebookfinal.com/download/sas-certification-prep-guide-advanced-
programming-for-sas-9-fourth-edition-sas-institute/
https://ebookfinal.com/download/pragmatic-unit-testing-in-c-with-
nunit-pragmatic-programmers-1st-edition-andy-hunt/
Programming Java 2 micro edition on Symbian OS a developer
s guide to MIDP 2 0 Martin De Jode
https://ebookfinal.com/download/programming-java-2-micro-edition-on-
symbian-os-a-developer-s-guide-to-midp-2-0-martin-de-jode/
https://ebookfinal.com/download/maple-9-advanced-programming-
guide-1st-edition-monagan/
https://ebookfinal.com/download/base-sas-9-1-procedures-guide-
volumes-1-2-3-and-4-1st-edition-sas-institute/
https://ebookfinal.com/download/essential-xna-game-
studio-2-0-programming-1st-edition-jim-perry/
https://ebookfinal.com/download/ruby-programming-1st-edition-jerry-
lee-ford/
Programming Ruby 1 9 2 0 The Pragmatic Programmers
Guide Fourth Edition Dave Thomas Digital Instant
Download
Author(s): Dave Thomas, Andy Hunt, Chad Fowler
ISBN(s): 9781937785499, 1937785491
Edition: Fourth Edition
File Details: PDF, 16.05 MB
Year: 2013
Language: english
Download from Wow! eBook <www.wowebook.com>
Download from Wow! eBook <www.wowebook.com>
Programming Ruby 1.9 & 2.0
The Pragmatic Programmers’ Guide
Dave Thomas
with Chad Fowler
Andy Hunt
Preface . . . . . . . . . . . . . . . . xi
Road Map . . . . . . . . . . . . . . . xv
2. Ruby.new . . . . . . . . . . . . . . . 15
2.1 Ruby Is an Object-Oriented Language 15
2.2 Some Basic Ruby 17
2.3 Arrays and Hashes 20
2.4 Symbols 21
2.5 Control Structures 23
2.6 Regular Expressions 24
2.7 Blocks and Iterators 25
2.8 Reading and ’Riting 27
2.9 Command-Line Arguments 28
2.10 Onward and Upward 28
6. Standard Types . . . . . . . . . . . . . . 83
6.1 Numbers 83
6.2 Strings 86
6.3 Ranges 90
7. Regular Expressions . . . . . . . . . . . . . 93
7.1 What Regular Expressions Let You Do 93
7.2 Ruby’s Regular Expressions 94
7.3 Digging Deeper 96
7.4 Advanced Regular Expressions 105
9. Expressions . . . . . . . . . . . . . . 125
9.1 Operator Expressions 126
9.2 Miscellaneous Expressions 127
9.3 Assignment 128
9.4 Conditional Execution 132
9.5 case Expressions 136
9.6 Loops 138
9.7 Variable Scope, Loops, and Blocks 142
Index . . . . . . . . . . . . . . . . 833
Why Ruby?
When Andy and I wrote the first edition, we had to explain the background and appeal of
Ruby. Among other things, we wrote, “When we discovered Ruby, we realized that we’d
found what we’d been looking for. More than any other language with which we have
worked, Ruby stays out of your way. You can concentrate on solving the problem at hand,
instead of struggling with compiler and language issues. That’s how it can help you become
a better programmer: by giving you the chance to spend your time creating solutions for
your users, not for the compiler.”
That belief is even stronger today. More than thirteen years later, Ruby is still my language
of choice: I use it for client applications and web applications. I use it to run our publishing
business (our online store, http://pragprog.com, is more than 40,000 lines of Rails code), and I
use it for all those little programming jobs I do just to get things running smoothly.
In all those years, Ruby has progressed nicely. A large number of methods have been added
to the built-in classes and modules, and the size of the standard library (those libraries
included in the Ruby distribution) has grown tremendously. The community now has a
standard documentation system (RDoc), and RubyGems has become the system of choice
for packaging Ruby code for distribution. We have a best-of-breed web application frame-
work, Ruby on Rails, with others waiting in the wings. We are leading the world when it
comes to testing, with tools such as RSpec and Cucumber, and we’re working through the
hard problems of packaging and dependency management. We’ve matured nicely.
But Ruby is older than that. The first release of this book happened on Ruby’s 20th birthday
(it was created on February 24, 1993). The release of Ruby 2.0 is a celebration of that
anniversary.
Ruby Versions
1
This version of the PickAxe documents both Ruby 2.0 and Ruby 1.9.3.
Exactly what version of Ruby did I use to write this book? Let’s ask Ruby:
$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.0]
This illustrates an important point. Most of the code samples you see in this book are actually
executed each time I format the book. When you see some output from a program, that
output was produced by running the code and inserting the results into the book.
Resources
Visit the Ruby website at http://www.ruby-lang.org to see what’s new. Chat with other Ruby
users on the newsgroup or mailing lists (see Appendix 1, Support, on page 829).
And I’d certainly appreciate hearing from you. Comments, suggestions, errors in the text,
and problems in the examples are all welcome. Email us at rubybook@pragprog.com.
2
If you find errors in the book, you can add them to the errata page. If you’re reading the
PDF version of the book, you can also report an erratum by clicking the link in the page
footers.
You’ll find links to the source code for almost all the book’s example code at http://www.prag-
prog.com/titles/ruby4.
1. Ruby version numbering used to follow the same scheme used for many other open source projects.
Releases with even minor version numbers—1.6, 1.8, and so on—were stable, public releases. These
are the releases that are prepackaged and made available on the various Ruby websites. Development
versions of the software had odd minor version numbers, such as 1.5 and 1.7. However, in 2007 Matz
broke with convention and made 1.9 a stable public release of Ruby.
2. http://www.pragprog.com/titles/ruby4/errata.html
Acknowledgments
The first International Ruby Conference had something like 32 attendees. We could all fit
into the tiny hotel bar and talk the night away. Things have changed. The annual conference
now sells out many hundreds of seats within hours, and an increasing number of secondary
conferences have sprung up to meet the needs of folks who can’t get to RubyConf.
As the community has grown, so has Ruby. The language and its libraries are now many
times bigger than they were back when the first edition of this book came out.
And as the language has grown, so has this book. The PickAxe is now massive, mostly
because I still want to document every single built-in class, module, and method. But a book
of this size can never be a solo undertaking. This edition builds on the work from the first
two editions, which included major contributions from Chad Fowler and Andy Hunt. Just
as significant, all three editions have been works created by the Ruby community. On the
mailing lists, in the forums, and on this book’s errata pages, hundreds of people have con-
tributed ideas, code, and corrections to make it better. As always, I owe every one of you a
big “thank you!” for all you have done and for all that you do. The Ruby community is still
as vibrant, interesting, and (mostly) friendly as it ever was—that’s quite an achievement
given the explosive growth we’ve enjoyed.
For the third (tenth anniversary) printing, Wayne E. Seguin was kind enough to check the
section on the wonderful tool RVM, and Luis Lavena checked the section on installing under
Windows, as well as the chapter on running Ruby on Windows. And I’d like to call Anthony
Burns a hero for doing an amazing job of reading through the changes as I was writing them,
3
but that would take away from the fact that he’s a true hero.
Getting this book into production has also been a challenge. Kim Wimpsett is the world’s
best copy editor—she’s the only copy editor I know who finds errors in code and fixes XML
markup. Any remaining errors in this book are a result of my mistyping her suggested cor-
rections. And, as we raced to get the book to the printer in time for RubyConf X, Janet Furlow
patiently kept us all on track.
Finally, I’m still deeply indebted to Yukihiro “Matz” Matsumoto, the creator of Ruby.
Throughout this prolonged period of growth and change, he has remained helpful, cheery,
and dedicated to polishing this gem of a language. The friendly and open spirit of the Ruby
community is a direct reflection of the person at its center.
Thank you all. Domo arigato gozaimasu.
Dave Thomas
The Pragmatic Programmers
dave@pragprog.com
June 2013
3. http://www.flickr.com/photos/pragdave/sets/72157625046498937/
Notation Conventions
Literal code examples are shown using a sans-serif font:
class SampleCode
def run
#...
end
end
Within the text, Fred#do_something is a reference to an instance method (in this case the method
4
do_something) of class Fred, Fred.new is a class method, and Fred::EOF is a class constant. The
decision to use a hash character to indicate instance methods was a tough one. It isn’t valid
Ruby syntax, but we thought that it was important to differentiate between the instance and
class methods of a particular class. When you see us write File.read, you know we’re talking
about the class method read. When instead we write File#read, we’re referring to the instance
method read. This convention is now standard in most Ruby discussions and documentation.
This book contains many snippets of Ruby code. Where possible, we’ve tried to show what
happens when they run. In simple cases, we show the value of expressions on the same line
as the expression. Here’s an example:
a = 1
b = 2
a + b # => 3
Here, you can see that the result of evaluating a + b is the value 3, shown to the right of the
arrow. Note that if you were to run this program, you wouldn’t see the value 3 output—
you’d need to use a method such as puts to write it out.
At times, we’re also interested in the values of assignment statements:
a = 1 # => 1
a + 2 # => 3
If the program produces more complex output, we show it after the program code:
3.times { puts "Hello!" }
produces:
Hello!
Hello!
Hello!
In some of the library documentation, we wanted to show where spaces appear in the output.
You’ll see these spaces as ␣ characters.
Command-line invocations are shown with literal text in a regular font, and parameters you
supply are shown in an italic font. Optional elements are shown in brackets.
ruby ‹ flags ›* progname ‹ arguments ›*
4. In some other Ruby documentation, you may see class methods written as Fred::new. This is perfectly
valid Ruby syntax; we just happen to think that Fred.new is less distracting to read.
to take the slower approach and keep going with the tutorial that follows, or you can skip
ahead to the gritty details starting in Part III, followed by the library reference in Part IV.
Experts, gurus, and “I-don’t-need-no-stinking-tutorial” types can dive straight into the lan-
guage reference in Chapter 22, The Ruby Language, on page 297; skim the library reference;
and then use the book as a (rather attractive) coffee coaster.
Of course, nothing is wrong with just starting at the beginning and working your way
through page by page.
And don’t forget, if you run into a problem that you can’t figure out, help is available. For
more information, see Appendix 1, Support, on page 829.
Facets of Ruby
Getting Started
Before we start talking about the Ruby language, it would be useful if we helped you get
Ruby running on your computer. That way, you can try sample code and experiment on
your own as you read along. In fact, that’s probably essential if you want to learn Ruby—
get into the habit of writing code as you’re reading. We will also show you some different
ways to run Ruby.
The path to a file or directory is the set of directories that you have to traverse to get to it
from the top-level directory, followed by the name of the file or directory itself. Each compo-
nent in this name is separated by a forward slash (on Unix) or a backslash (on Windows).
So, if you organized your projects in a directory called projects under the top-level directory
and if the projects directory had a subdirectory for your time_planner project, the full path to
the README file would be /projects/time_planner/readme.txt on Unix and C:\projects\time_plan-
ner\readme.txt on Windows.
To navigate to a directory, use the cd command. (Because the Unix prompt varies from system
to system, we’ll just use a single dollar sign to represent it here.)
$ cd /projects/time_planner (on Unix)
C:\> cd \projects\time_planner (on Windows)
On Unix boxes, you probably don’t want to be creating top-level directories. Instead, Unix
gives each user their own home directory. So, if your username is dave, your home directory
might be located in /usr/dave, /home/dave, or /Users/dave. At the shell prompt, the special char-
acter ~ (a single tilde) stands for the path to your home directory. You can always change
directories to your home directory using cd ~, which can also be abbreviated to just cd.
To find out the directory you’re currently in, you can type pwd (on Unix) or cd on Windows.
So, for Unix users, you could type this:
$ cd /projects/time_planner
$ pwd
/projects/time_planner
$ cd
$ pwd
/Users/dave
You can create a new directory under the current directory using the mkdir command:
$ cd /projects
$ mkdir expense_tracker
$ cd expense_tracker
$ pwd
/projects/expense_tracker
Notice that to change to the new directory, we could just give its name relative to the current
directory—we don’t have to enter the full path.
We suggest you create a directory called pickaxe to hold the code you write while reading
this book:
$ mkdir ~/pickaxe (on Unix)
C:\> mkdir \pickaxe (on Windows)
Get into the habit of changing into that directory before you start work:
$ cd ~/pickaxe (on Unix)
C:\> cd \pickaxe (on Windows)
Installing on Windows
There are two options for installing Ruby on Windows. The first is a simple installer pack-
age—download it, and you’ll have Ruby up and running in minutes. The second is slightly
more complex but gives you the flexibility of easily managing multiple Ruby environments
on the same computer at the same time. Whichever option you choose, you’ll first need to
download and install a working Ruby.
Select Start Command Prompt with Ruby to open a copy of the Windows command shell with
the environment set up to run Ruby.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com