Académique Documents
Professionnel Documents
Culture Documents
Programming Ruby
The Pragmatic Programmers’ Guide
Second Edition
This PDF file contains pages extracted from Programming Ruby, published by The Pragmatic Bookshelf.
For more information, visit http://www.pragmaticbookshelf.com.
Note: This extract contains some colored text. is available only in online versions of the books. The printed
versions are black and white. Pagination might vary between the online and printer versions; the content is
otherwise identical.
Copyright © 2004 The Pragmatic Programmers, LLC.
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.
Contents
F OREWORD TO THE F IRST E DITION xix
F OREWORD TO THE S ECOND E DITION xxi
P REFACE xxii
ROAD M AP xxviii
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
C S UPPORT 758
Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Download Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Usenet Newsgroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Mailing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
D B IBLIOGRAPHY 761
I NDEX 762