The Well-Grounded Rubyist, Second Edition
David A. Black
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 261
Shelter Island, NY 11964
Email: orders@manning.com
2014 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 | Development editor: Dan Maharry Technical development editor: Andrew Gibson Copyeditor: Jodie Allen Proofreader: Melody Dolab Technical proofreader: Deepak Vohra Typesetter: Dennis Dalinnik Cover designer: Marija Tudor
|
ISBN: 9781617291692
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 EBM 19 18 17 16 15 14
Dedication
To Barbara Aronstein Black, and in memory of Charles L. Black, Jr. (19152001), with love. Thanks for the writing genes.
Brief Table of Contents
Table of Contents
Praise for the First Edition
If you need to learn the Ruby language, stop right here! This book is all you need. John Griffin, Overstock.com David Blacks understanding of Ruby and his ability to transfer that knowledge is phenomenal. Philip Hallstrom, PJKH, LLC The long-awaited follow-up to Davids Ruby for Rails, this time with the emphasis on Ruby. Rick DeNatale, DenHaven Consulting A must-read for all who want to use Ruby to learn object orientation or programming in general. Robert Klemme, Orga Systems David Black again creates a reference that belongs on every Ruby developers desk. Robert Dempsey, Atlantic Dominion Solutions By far the best book if you want to learn Ruby. M. Edward (Ed) Borasky, ADP The inner workings of Ruby? Its a secret revealed by David Black in this book. Patrick Steger, Zhlke Engineering AG A solid, example-based Ruby tutorial with outstanding code examplesgreat for learning the language. Doug Warren, Java Web Services I wish I had had this book when I was learning Rails. Patrick Dennis, Management Dynamics, Inc. A great follow-up to Ruby for Rails. Mark Eagle, Intercontinental Exchange, Inc. Highly recommended to anyone learning Ruby. Greg Donald, VUMC An excellent and thorough introduction to the Ruby language. Curtis Miller, Freebootr.com
Preface
Welcome to the second edition of The Well-Grounded Rubyist . In the five years since the first edition came out, the Ruby language has changed and evolvedmost notably with the release, on Rubys twentieth birthday (February 24, 2013), of Ruby 2.0. At the end of 2013Christmas, to be preciseRuby 2.1 was released; thats the version covered in this edition.
The Well-Grounded Rubyist has been very well-received. It seemed to hit a sweet spot for a lot of readers, including many who were completely new to Ruby, as well as many who had done some Ruby already but wanted to make a thorough pass through the language and make sure they really understood it. Ive been particularly pleased by those comments that describe the experience of reading the book as being like working directly with a Ruby teacher. After all, I am a Ruby teacher, and though teaching involves wearing a different hat than writing, the two hats arent all that different after all.
Much hasnt changed in Rubybut quite a lot has. This second edition includes a lot of new material, along with an extensive review and reworking of the original text. As with the first edition, the book doesnt pretend to be an exhaustive Ruby and standard library reference (those are available elsewhere), but an in-depth examination and explication of the key aspects of Rubys object model, built-in classes, and important programming facilities.
Id like to extend a warm welcome to all of the books readers, including those who read the first edition and those who are entirely new to The Well-Grounded Rubyist . I hope and trust that youll find a great deal to interest and excite you here. Enjoy!
Preface to the First Edition
In 2006, Manning published my book Ruby for Rails: Ruby Techniques for Rails Developers . My goal in writing Ruby for Rails or, as it has come to be known, R4R was to provide Rails developers with both an understanding of the fact that being a Rails developer means being a Ruby developer, and a solid grasp of Ruby. I chose Ruby topics for inclusion (or exclusion) based on my judgment as to their relative importance for people who wanted to learn Ruby mainly in order to use Rails correctly and effectively.
Critical response to R4R was very good. The book filled a void: it was neither just a Ruby book nor just a Rails book, but a Ruby book optimized, so to speak, for the Rails developer. I was pleased by the books receptionand particularly by the many people who, after reading it, asked me whether I had any plans to write a whole book just about Ruby, and encouraged me to write one.
And that, to make a long story short, is what I have done.
The Well-Grounded Rubyist is a just Ruby book, and its written to be read by anyone interested in Ruby. Its a descendant of R4R but not exactly an update. Its more of a repurposing. Theres some overlap with R4R , but theres also a lot of new material (more than I originally anticipated, in fact); and everything, overlap or not, has been oiled and polished and spiffed up to work with Ruby 1.9.1, the newest version of Ruby (and very new) at the time the book went to press.
Mind you, I dont mean for Rails developers not to read The Well-Grounded Rubyist . On the contrary: Im optimistic that in the three years since R4R was published, the idea that Rails developers should learn Ruby has become commonplace, and many people who first got into Ruby through Rails have gotten interested in Ruby in its own right. I want this book to be there waiting for themand for the many people who are discovering Ruby through completely different channels.
So whatever brings you here, I hope you enjoy the book.
Acknowledgments
Thanks first of all to everyone whom I thanked in the first edition. This edition would not exist if it were not for that ensemble of editors, production personnel, reviewers, publishers, and colleagues.
For the second edition my thanks go first and foremost to development editor Dan Maharry, whose patience, support, and guidance kept the book on track when it felt like it might otherwise not be. Technical development editor Andrew Gibson cast an expert and expertly critical eye on the entire book, to its great benefit. Copyeditor Jodie Allen contributed greatly to the process of putting an overall polish on the text. Technical proofreader Deepak Vohra meticulously examined the sample code, and spotted some problems in a timely manner. Proofreader Melody Dolab contributed an adept final read-through and correction of the entire text.
Next page