Metaprogramming Ruby 2
Program Like the Ruby Pros
by Paolo Perrotta
Version: P1.0 (August 2014)
Copyright 2014 The Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
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.
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://pragprog.com.
The team that produced this book includes:
Lynn Beighley (editor)
Potomac Indexing, LLC (indexer)
Cathleen Small (copyeditor)
Dave Thomas (typesetter)
Janet Furlow (producer)
Ellie Callahan (support)
For international rights, please contact .
For the Best Reading Experience...
We strongly recommend that you read this book with the publisher defaults setting enabled for your reading device or application. Certain formats and characters may not display correctly without this setting. Please refer to the instructions for your reader on how to enable the publisher defaults setting.
I was thirteen, and I was tired of hanging
out at the local toy shop to play Intellivision games. I wanted my own videogame console. Id been bugging my parents for a while,
with no success.
Then I found an alternative: I could play games on a computeras well. So I asked my parents to buy me one of those new8-bit computersyou know, to learn useful stuff. My dadagreed, and my mom took me to the shop and bought me aSinclair ZX Spectrum.
Mom, Dad Here is something that I shouldve told youmore often in my life: thank you. This book is dedicated tothe two of you. Im hoping it will make you proud, just likeyour once-kid is proud of you. And while Im here, I havesomething to confess about that life-changing day thirty yearsago: I didnt really want to learn stuff. I just wanted toplay.
In fact, thats what Ive been doing
all these years.
Table of Contents
Copyright 2014, The Pragmatic Bookshelf.
What Readers Are Saying About Metaprogramming Ruby 2
This is the one book about Ruby that makes you go So thats how it works over and over again, as concepts such as the object model, DSLs, and blocks fall into place with that satisfying Click! sound. Its a great guide to what happens under the hood of a language that seems to involve a kind of magic deep inside. I highly recommend it.
Peter Bakhirev |
Lead software engineer, Gilt City |
The previous edition of Metaprogramming Ruby changed my life and my code, and helped me get my first programming job. You would think there would be no way to improve on a book that good, but Paolo Perrotta has done it. Learn to unlock the hidden potential of this beautiful language, and fall in love with Ruby again.
Richard Schneeman |
Programmer, Heroku |
For gem authors and application developers alike, this book lays down the foundation everyone needs to harness the full power of Ruby. Paolo describes metaprogramming in a fun and approachable way for all skill levels. The knowledge garnered from reading this book will help you write cleaner code and work more effectively with legacy codebases.
Paul Elliott |
Rocketeer, Hashrocket |
If you want to follow the path of Ruby metaprogramming mastery, then this book is the best companion you can think of, no matter what your level is. I had struggled with Ruby metaprogramming for years until I read this book; now it all makes sense.
Fabien Catteau |
Software developer, Tech-Angels |
This is a book that everyone who wants to have a deeper understanding of the inner workings of Ruby and Ruby on Rails should read. The spells described in this book are invaluable tools to understand and use Ruby to its full extent. This is not only about metaprogramming, but also about taking your Ruby programming to a different level.
Kosmas Chatzimichalis |
Software engineer |
Im a huge Python fan, so I was supposed to disregard Ruby. Paolo made me appreciate it. Not only is Metaprogramming Ruby the book that allowed me to wrap my head around this esoteric and fascinating topic, but it also made me rethink the way I write code in other languages.
Arialdo Martini |
Programmer, JobRapido.com |
This book uncovers all the ins and outs of the art of metaprogramming in Ruby, with a no-nonsense approach and an irony that transpires from vibrant prose, never boring, without compromising any of its insightfulness. Metaprogramming Ruby is one of those books that any serious Rubyist (and even the pros) will want to revisit from time to time.
Piergiuliano Bossi |
Principal engineer lead, Points |
Metaprogramming Ruby has been a hugely influential book for me, especially during a time when I wanted to learn about the inner workings of Ruby. Paolo Nusco Perrotta made what is normally a complex topic fun, enjoyable, and very approachable.
Josh Kalderimis |
CEO, Travis CI |
Foreword
Ruby inherits characteristics from various languagesLisp, Smalltalk, C, and Perl, to name a few. Metaprogramming comes from Lisp (and Smalltalk). Its a bit like magic, which makes something astonishing possible. There are two kinds of magic: white magic, which does good things, and black magic, which can do nasty things. Likewise, there are two aspects to metaprogramming. If you discipline yourself, you can do good things, such as enhancing the language without tweaking its syntax by macros or enabling internal domain-specific languages. But you can fall into the dark side of metaprogramming. Metaprogramming can confuse easily.
Ruby trusts you. Ruby treats you as a grown-up programmer. It gives you great power, such as metaprogramming. But you need to remember that with great power comes great responsibility.
Enjoy programming in Ruby.
matz
Copyright 2014, The Pragmatic Bookshelf.
Acknowledgments
Thank you, Joe Armstrong, Satoshi Asakawa, Peter Bakhirev, Paul Barry, Juanjo Bazn, Emmanuel Bernard, Roberto Bettazzoni, Ola Bini, Piergiuliano Bossi, Simone Busoli, Alessandro Campeis, Kosmas Chatzimichalis, Andrea Cisternino, Davide DAlto, Pietro Di Bello, Mauro Di Nuzzo, Marco Di Timoteo, Paul Elliott, Eric Farkas, Mauricio Fernandez, Francisco Fernndez Castao, Jay Fields, Michele Finelli, Neal Ford, Florian Frank, Sanne Grinovero, Federico Gobbo, Florian Gro, Sebastian Hennebrder, Doug Hudson, Jurek Husakowski, Lyle Johnson, Lisa Maria Jones, Josh Kalderimis, Murtuza Kutub, Marc Lainez, Daniele Manni, Luca Marchetti, Arialdo Martini, Kado Masanori, MenTaLguY, Nicola Moretto, Sandro Paganotti, Alessandro Patriarca, Carlo Pecchia, Susanna Perrotta, John Pignata, Andrea Provaglio, Mike Roberts, Martin Rodgers, , Richard Schneeman, Joe Sims, Jeremy Sydik, Andrea Tomasini, Mauro Tortonesi, Marco Trincardi, Ivan Vaghi, Giancarlo Valente, Davide Varvello, Elzie Vergine.