Programming Ruby The Pragmatic Programmers Guide 2 nd edition Dave Thomas 2024 scribd download
Programming Ruby The Pragmatic Programmers Guide 2 nd edition Dave Thomas 2024 scribd download
https://ebookgate.com
https://ebookgate.com/product/programming-ruby-
the-pragmatic-programmers-guide-2-nd-edition-dave-
thomas/
https://ebookgate.com/product/programming-ruby-3-3-the-pragmatic-
programmers-guide-5-converted-edition-noel-rappin/
ebookgate.com
https://ebookgate.com/product/programming-ruby-1-9-3rd-edition-dave-
thomas/
ebookgate.com
https://ebookgate.com/product/pragmatic-thinking-and-learning-
refactor-your-wetware-pragmatic-programmers-1st-edition-andy-hunt/
ebookgate.com
https://ebookgate.com/product/programming-elixir-1-3-functional-
concurrent-pragmatic-fun-1st-edition-david-thomas/
ebookgate.com
The Ruby Programming Language 1st Edition David Flanagan
https://ebookgate.com/product/the-ruby-programming-language-1st-
edition-david-flanagan/
ebookgate.com
https://ebookgate.com/product/programming-visual-basic-net-1st-ed-
edition-dave-grundgeiger/
ebookgate.com
https://ebookgate.com/product/opengl-es-2-0-programming-guide-1st-
edition-aaftab-munshi/
ebookgate.com
Developers the world over talk about
Programming Ruby and the Ruby language. . .
“Ruby is a wonderfully powerful and useful language, and whenever I’m working
with it, this book is at my side.”
Martin Fowler, Chief Scientist, ThoughtWorks
“If your world revolves around Java, as mine did, then you need this outstanding book
to learn all the wonderful things you’re missing. There’s just one catch: you’ll be
spoiled from then on. Indeed, after reading just a few pages of Programming Ruby,
programming in any language other than Ruby will feel like you’re pushing rope.”
Mike Clark, Author and Consultant
“Ruby is smart, elegant, and fun, and it deserves a book that’s smart, elegant, and fun.
The first edition of Programming Ruby was such a book; the second edition is even
better.”
James Britt, Administrator, http://ruby-doc.org
“The best reason to learn a new programming language is to learn to think differently.
The best way to learn to think the Ruby way is to read Programming Ruby. Several
years ago, with the first edition of this book, I did just that. Since then, I’ve had a
constant stream of enjoyable Ruby programming experiences. This is due in no
insignificant part to the quality of the source from which I learned the language. I’m
not the only person I’ve heard say that every language should have a book like this.”
Chad Fowler, Codirector, Ruby Central, Inc.
“The PickAxe got me started on Ruby. It is still the first book I turn to.”
Ryan Davis, Founder, Seattle.rb
“This book changed my life. Sounds rather clichéd, but it’s the truth. After six years
and 300,000 lines of Java code, I needed a change. That change occurred upon reading
the first edition of this book. With the support of a solid community and ever-growing
foundation of superb libraries, I founded a company that largely profits from applying
Ruby to solve real-world problems. Ruby is ready for prime time, and this new
version of the PickAxe will show a waiting world what a gem Ruby really is.”
Rich Kilmer, President and CEO, InfoEther LLC
“The first edition of PickAxe has been a desk-side companion for years. The second
edition will be an eagerly awaited replacement.”
Tom Enebo, JRuby Developer
“The first edition of Programming Ruby brought about no less than the introduction of
Ruby on a large scale outside of Japan, in the process becoming the de facto standard
published language reference and an oft-cited model of clear, effective technical
writing. The appearance of the second, expanded edition is exciting for Ruby
programmers around the world and will no doubt attract a fresh wave of newcomers to
this elegant, versatile language.”
David A. Black, Ph.D., Codirector, Ruby Central, Inc.
“Ruby is my definite choice for all scripting and prototyping issues, and this book will
help you to discover its usefulness as well as its beauty. Apart from that, it’s really fun
to read!”
Robert Klemme
“I bought the first edition of this book the day it was released and had a fantastic time
using it to learn Ruby. I eventually bought a second copy to keep at home. But Ruby
has changed since then. I’m delighted that this second edition of Programming Ruby
is available to help a new round of programmers learn about this fantastic, beautiful
language. And it’s not just good news for Ruby newbies, of course—like me, most
Ruby developers will want a copy (no, make that two) so that all of the details about
today’s Ruby will be close at hand.”
Glenn Vanderburg, Software Architect, Countrywide Financial
“Ruby is one of those great languages that takes an afternoon to start using and years
(maybe a lifetime) to master. In C, I’m always having to work around the limitations
of the language; in Ruby, I’m always discovering a neater, cleaner, more efficient way
to do things. Programming Ruby is the essential reference to the Ruby language. More
than just teaching you the syntax, it teaches you the spirit and the feel of the language.”
Ben Giddings
“Confucius said, “What you hear, you forget.” He also said, “What you do you
understand.” But it’s not easy to actually “do” things unless you’re using a great
language with strength in quick and clean prototyping. In my case, this language is
Ruby! Thank you!”
Michael Neumann
Programming Ruby
The Pragmatic Programmers’ Guide
Second Edition
Dave Thomas
with Chad Fowler
and Andy Hunt
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility
for errors or omissions or for damages that may result from the use of information (including program
listings) contained herein.
This book is a heavily revised version of the book Programming Ruby, originally published by Addison
Wesley. This book is printed with their permission.
Our Pragmatic courses, workshops, and other products can help you and your team create better software
and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at
http://www.pragmaticprogrammer.com
Copyright © 2005 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be
reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical,
photocopying, recording, or otherwise, without the prior consent of the publisher.
ISBN 0-9745140-5-5
Text printed on acid-free paper.
First Printing, October 2004
Version: 2004-9-30
Contents
F OREWORD TO THE F IRST E DITION xvii
F OREWORD TO THE S ECOND E DITION xix
P REFACE xx
ROAD M AP xxvi
2 RUBY. NEW 9
Ruby Is an Object-Oriented Language . . . . . . . . . . . . . . . . . . . 9
Some Basic Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Arrays and Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Blocks and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Reading and ’Riting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Onward and Upward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5 S TANDARD T YPES 55
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7 E XPRESSIONS 81
Operator Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Miscellaneous Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 83
Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Case Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Variable Scope, Loops, and Blocks . . . . . . . . . . . . . . . . . . . . . 99
9 M ODULES 110
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Iterators and the Enumerable Module . . . . . . . . . . . . . . . . . . . . 113
Composing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Including Other Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
19 RUBY T K 241
Simple Tk Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Binding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Translating from Perl/Tk Documentation . . . . . . . . . . . . . . . . . . 251
Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
ObjectSpace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Proc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Process::GID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Process::Status . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Process::Sys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Process::UID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Struct::Tms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
ThreadGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
TrueClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
UnboundMethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
FileUtils . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Forwardable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
ftools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
GDBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
GetoptLong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
GServer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Iconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
IO/Wait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
IPAddr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
jcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
mathn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
Mutex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Mutex_m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
Net::FTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Net::HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
Net::IMAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Net::POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Net::SMTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
Net::Telnet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
NKF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Observable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
open-uri . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
Open3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
OpenSSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
OpenStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
OptionParser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
ParseDate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Pathname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
PP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
PrettyPrint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
Profiler_ _ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
PStore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
PTY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
Rational . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
readbytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Readline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Resolv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
REXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Rinda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
RSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
Scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
SDBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
Shellwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Singleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
StringIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
StringScanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
Sync . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Syslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Tempfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Test::Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
ThreadsWait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
Timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Tk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
tmpdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Tracer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
TSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
un . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
URI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
WeakRef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
WEBrick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
Win32API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
WIN32OLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
XMLRPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
YAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
Zlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
C S UPPORT 758
Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Download Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Usenet Newsgroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Mailing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
D B IBLIOGRAPHY 761
I NDEX 762
combined efforts of many people. Without their help, Ruby could never have become
what it is.
I am especially thankful to the authors of this book, Dave Thomas and Andy Hunt.
Ruby has never been a well-documented language. Because I have always preferred
writing programs over writing documents, the Ruby manuals tend to be less thorough
than they should be. You had to read the source to know the exact behavior of the
language. But now Dave and Andy have done the work for you.
They became interested in a lesser-known language from the Far East. They researched
it, read thousands of lines of source code, wrote uncountable test scripts and e-mails,
clarified the ambiguous behavior of the language, found bugs (and even fixed some of
them), and finally compiled this great book. Ruby is certainly well documented now!
Their work on this book has not been trivial. While they were writing it, I was modi-
fying the language itself. But we worked together on the updates, and this book is as
accurate as possible.
It is my hope that both Ruby and this book will serve to make your programming easy
and enjoyable. Have fun!