Thông tin tài liệu
There may be no better way to learn how to program
than by dissecting real, representative examples written
in your language of choice. In Ruby by Example, author
Kevin Baird analyzes 44 Ruby scripts, offering step-by-
step explanations of how the code works and how to
modify it to fit your needs.
Baird’s examples demonstrate key features of the
language (such as inheritance, encapsulation, higher-
order functions, and recursion), while simultaneously
solving difficult problems (such as validating XML,
creating a bilingual program, and creating command-
line interfaces). Each chapter builds upon the next,
and each key concept is highlighted in the margin to
make it easier for you to navigate the book.
You’ll learn how to:
• Use the interactive Ruby shell (irb) to learn key
features of the language
• Extend Ruby using RubyGems, the Ruby package
manager
• Create numerical utilities, as well as utilities that
process and analyze HTML/XML
• Implement purely functional and metaprogramming
techniques to save time and effort
www.nostarch.com
TH E FI N EST I N G E E K E N T E RTA I N M E NT
™
SHELVE IN:
PROGRAMMING/RUBY
$34.95 ($43.95 CDN)
®
• Optimize, profile, and test your code to make sure
that it not only does its job, but does it well
• Create complex utilities that model natural selection,
discover mysterious patterns in the Bible (and in
Moby-Dick) that “predict” the future, and pick songs
to play for a radio station
• Create web applications using Rails
Ruby is the fastest growing programming language
today, and for good reason: Its elegant syntax
and readable code make for prolific and happy
programmers. But it can be difficult to understand
and implement without a little help. Ruby by Example
shows you how to take advantage of Ruby as you
explore Ruby’s fundamental concepts in action.
A B O U T T H E A U T H O R
Kevin C. Baird received his Ph.D. from the State
University of New York at Buffalo. He originally wrote
his dissertation in Python but rewrote the project after
discovering Ruby, and he hasn’t looked back since.
He has presented at RubyConf and written articles
for Linux Journal, Music & Computers magazine, and
the New Interfaces for Musical Expression conference
proceedings.
E X P L O R I N G
F U N C T I O N A L
C O D E
E X P L O R I N G
F U N C T I O N A L
C O D E
“I LAY F L A T .”
This book uses RepKover — a durable binding that won’t snap shut.
Printed on recycled paper
R U B Y
B Y E X A M P L E
R U B Y
B Y E X A M P L E
K E V I N C . B A I R D
®
C O N C E P T S A N D C O D E
R U B Y B Y E X A M P L E
R U B Y B Y E X A M P L E
B A I R D
RUBY BY EXAMPLE
RUBY BY EXAMPLE
Concepts and Code
by Kevin C. Baird
San Francisco
®
RUBY BY EXAMPLE. Copyright © 2007 by Kevin C. Baird.
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.
11 10 09 08 07 1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-148-4
ISBN-13: 978-1-59327-148-0
Publisher: William Pollock
Production Editor: Elizabeth Campbell
Cover and Interior Design: Octopod Studios
Developmental Editor: Tyler Ortman
Technical Reviewer: Pat Eyler
Copyeditor: Megan Dunchak
Compositors: Christina Samuell and Riley Hoffman
Proofreader: Publication Services, Inc.
Indexer: Nancy Guenther
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
555 De Haro Street, Suite 250, San Francisco, CA 94107
phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Baird, Kevin C.
Ruby by example : concepts and code / Kevin C. Baird.
p. cm.
Includes index.
ISBN-13: 978-1-59327-148-0
ISBN-10: 1-59327-148-4
1. Object-oriented programming (Computer science) 2. Ruby (Computer program language) I. Title.
QA76.64.B27 2007
005.1'17 dc22
2007018653
No Starch Press and the No Starch Press logo are registered trademarks 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.
Printed on recycled paper in the United States of America
This book is dedicated to my parents, who bought
the first computer I ever programmed.
BRIEF CONTENTS
Acknowledgments xvii
Introduction: What Is Ruby? xix
Chapter 1: Interactive Ruby and the Ruby Environment 1
Chapter 2: Amusements and Simple Utilities 13
Chapter 3: Programmer Utilities 33
Chapter 4: Text Manipulation 51
Chapter 5: Number Utilities 71
Chapter 6: Functionalism with Blocks and Procs 99
Chapter 7: Using, Optimizing, and Testing Functional Techniques 121
Chapter 8: HTML and XML Tools 141
Chapter 9: More Complex Utilities and Tricks, Part I 161
Chapter 10: More Complex Utilities and Tricks, Part II 185
Chapter 11: CGI and the Web 205
Chapter 12: RubyGems and Rails Preparation 223
Chapter 13: A Simple Rails Project 237
Appendix: How Does Ruby Compare to Other Languages? 261
Index 267
[...]... version when you download Ruby. ) cp ruby- 1.8.4.tar.gz /usr/local/src/ cd /usr/local/src tar -xzf ruby- 1.8.4.tar.gz cd ruby- 1.8.4 Then follow the instructions in the README file The usual set of commands for installation is as follows ./configure make make install 2 For more on Ruby s ancestry, refer to the Ruby- Talk archives (http://blade.nagaokaut.ac jp/cgi-bin/scat.rb /ruby/ ruby-talk/179642) and O’Reilly’s... www.linuxdevcenter.com/pub/a/linux/2001/11/29 /ruby. html) xx In t rod uc ti on : Wh at I s R ub y ? You should now have a working version of Ruby You can test this by executing this command: ruby version If it reports ruby 1.8.4 (2005-12-24) [i486-linux] or whichever version you downloaded and your system, everything worked.3 On a Windows System If you use a Windows system, a One-Click Ruby Installer is available at http://rubyinstaller.rubyforge.org/wiki/wiki.pl... start using Ruby for some interesting tasks But before we start creating separate program files, we’ll explore how Ruby works with the Interactive Ruby environment In t rod uc ti on : Wh at I s R ub y? xxiii 1 INTERACTIVE RUBY AND THE RUBY ENVIRONMENT In Ruby, as in most programming languages, we’ll generally store programs in external files and execute them at once, as a unit However, Ruby also gives... or how verbose to be about warnings We’ve already seen an example of this in the instructions for installing Ruby from a source download, when we executed this command: ruby version As you’d expect, that just asks Ruby to report its version You can find out the various command-line options that Ruby understands by executing this command: ruby -h 10 C ha pt er 1 ... CGI and the Web This chapter talks about the Common Gateway Interface (CGI) and how to embed Ruby code in web documents xxii I n tr od uct io n: Wh at I s R uby? Chapter 12: RubyGems and Rails Preparation This chapter shows you how to use RubyGems, Ruby' s package manager, and uses that system to install Rails, Ruby' s main web development framework Chapter 13: A Simple Rails Project This chapter contains... Smalltalk—they were so influential, in fact, that he has jokingly referred to Ruby as MatzLisp On the other hand, some Ruby aficionados stress Ruby s 1 According to http:/ /ruby- lang.org similarities with Smalltalk and Perl, as did David Heinemeier Hansson, creator of Rails, in a June 2006 Linux Journal interview Hansson also describes Ruby as “a language for writing beautiful code that makes programmers happy.”... interested in learning more about Ruby s heritage, see the appendix for a comparison of Ruby to other languages Acquiring and Configuring Ruby But enough with the history—let’s set these questions aside and actually get Ruby installed It’s flexible, expressive, and released under a free software/open source license (The license is available online at http://www .ruby- lang.org/en/ about/license.txt.)... a bit about what you’ll find inside the chapters: Chapter 1: Interactive Ruby and the Ruby Environment This chapter describes Interactive Ruby (irb), and also introduces some key Ruby concepts Chapter 2: Amusements and Simple Utilities This chapter has our first stand-alone programs (or scripts) that continue introducing key Ruby concepts while accomplishing simple tasks Chapter 3: Programmer Utilities... download Ruby for your system It’s a comprehensive package—check the site for the most up-to-date list of its contents At the time of this writing, it included the base language with various popular extensions, including SciTE (a syntax-highlighting text editor), FreeRIDE (a Ruby development environment), a help file containing Dave Thomas’ book Programming Ruby (also called The Pickaxe), and the RubyGems... like mathematical relationships Ruby is flexible, meaning that you can program in any of these styles; however, it is primarily object oriented, with some strong functional influence This book focuses slightly more on the functional aspects of Ruby than some other books 3 Ruby, like most open source languages, is under constant development The code in this book uses Ruby version 1.8.4, which was the . P L E
B A I R D
RUBY BY EXAMPLE
RUBY BY EXAMPLE
Concepts and Code
by Kevin C. Baird
San Francisco
®
RUBY BY EXAMPLE. Copyright © 2007 by Kevin C. Baird.
All. how to:
• Use the interactive Ruby shell (irb) to learn key
features of the language
• Extend Ruby using RubyGems, the Ruby package
manager
• Create
Ngày đăng: 22/03/2014, 09:20
Xem thêm: RUBY BY EXAMPLE pot, RUBY BY EXAMPLE pot, #1 Is It Payday? (check_payday.rb), #3 The 99 Bottles of Beer Song (99bottles.rb), #4 Sound File Player (shuffle_play.rb), #5 What Is Truth? (boolean_golf.rb), #6 Making a List (array_join.rb), #9 End-of-Line Conversion (dos2unix.rb), #11 Wrapping Lines of Text (softwrap.rb), #12 Counting Words in a File (word_count.rb), #13 Word Histogram (most_common_words.rb), #14 Rotating Characters in a String (rotate.rb), #15 Computing Powers (power_of.rb), #16 Adding Commas to Numbers (commify.rb), #17 Roman Numerals (roman_numeral.rb), #18 Currency Conversion, Basic (currency_converter1.rb), #19 Currency Conversion, Advanced (currency_converter2.rb), #22 Using Procs for Compounded Filtering (matching_compound_members.rb), #23 Returning Procs as Values (return_proc.rb), #25 Procs for Text (willow_and_anya.rb), #27 Benchmarking and Profiling (tests/test_opts.rb), #28 Converting Temperatures (temperature_converter.rb), #30 Cleaning Up HTML (html_tidy.rb), #31 Counting Tags (xml_tag_counter.rb), #33 Validating XML (xml_well_formedness_checker.rb), #34 Finding Codes in the Bible or Moby-Dick (els_parser.rb), #35 Mutating Strings into Weasels (methinks.rb), #36 Mutating the Mutation of Strings into Weasels (methinks_meta.rb), #37 Overnight DJ (radio_player1.rb), #38 Better Overnight DJ (radio_player2.rb), #39 Numbers by Name (to_lang.rb), #40 Elegant Maps and Injects (symbol.rb), #41 A Simple CGI Script (simple_cgi.rb), #43 CSS Stylesheets, Part I (stylesheet.rcss), #44 CSS Stylesheets, Part II (stylesheet2.rcss)