ebook img

Metaprogramming Ruby 2: Program Like the Ruby Pros PDF

262 Pages·2014·14.41 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Metaprogramming Ruby 2: Program Like the Ruby Pros

What Readers Are Saying About Metaprogramming Ruby 2 This is the one book about Ruby that makes you go “So that’s 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. It’s 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 foun- dation 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 I’m a huge Python fan, so I was supposed to disregard Ruby. Paolo made me ap- preciate 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 Metaprogramming Ruby 2 Program Like the Ruby Pros Paolo Perrotta The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina 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, PragProg and the linking g device are trade- marks 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 [email protected]. Copyright © 2014 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. Printed in the United States of America. ISBN-13: 978-1-94122-212-6 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—August 2014 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. I’d been bugging my parents for a while, with no success. Then I found an alternative: I could play games on a computer as well. So I asked my parents to buy me one of those new 8-bit computers—you know, to learn useful stuff. My dad agreed, and my mom took me to the shop and bought me a Sinclair ZX Spectrum. Mom, Dad… Here is something that I should’ve told you more often in my life: thank you. This book is dedicated to the two of you. I’m hoping it will make you proud, just like your once-kid is proud of you. And while I’m here, I have something to confess about that life-changing day thirty years ago: I didn’t really want to learn stuff. I just wanted to play. In fact, that’s what I’ve been doing all these years. Contents Foreword . . . . . . . . . . . . . xiii Acknowledgments . . . . . . . . . . . xv Introduction . . . . . . . . . . . . xvii Part I — Metaprogramming Ruby 1. The M Word . . . . . . . . . . . . . 3 Ghost Towns and Marketplaces 3 The Story of Bob, Metaprogrammer 4 Metaprogramming and Ruby 7 2. Monday: The Object Model . . . . . . . . . 11 Open Classes 11 Inside the Object Model 16 Quiz: Missing Lines 26 What Happens When You Call a Method? 27 Quiz: Tangle of Modules 39 Wrap-Up 42 3. Tuesday: Methods . . . . . . . . . . . 45 A Duplication Problem 46 Dynamic Methods 48 method_missing 55 Quiz: Bug Hunt 64 Blank Slates 66 Wrap-Up 69 4. Wednesday: Blocks . . . . . . . . . . . 73 The Day of the Blocks 73 Quiz: Ruby# 75 Contents • x Blocks Are Closures 77 instance_eval() 85 Callable Objects 88 Writing a Domain-Specific Language 96 Quiz: A Better DSL 98 Wrap-Up 103 5. Thursday: Class Definitions . . . . . . . . 105 Class Definitions Demystified 106 Quiz: Class Taboo 112 Singleton Methods 113 Singleton Classes 118 Quiz: Module Trouble 129 Method Wrappers 131 Quiz: Broken Math 136 Wrap-Up 137 6. Friday: Code That Writes Code . . . . . . . . 139 Coding Your Way to the Weekend 139 Kernel#eval 141 Quiz: Checked Attributes (Step 1) 150 Quiz: Checked Attributes (Step 2) 153 Quiz: Checked Attributes (Step 3) 154 Quiz: Checked Attributes (Step 4) 156 Hook Methods 157 Quiz: Checked Attributes (Step 5) 160 Wrap-Up 162 7. Epilogue . . . . . . . . . . . . . 163 Part II — Metaprogramming in Rails 8. Preparing for a Rails Tour . . . . . . . . . 167 Ruby on Rails 168 Installing Rails 168 The Rails Source Code 168 9. The Design of Active Record . . . . . . . . 171 A Short Active Record Example 171 How Active Record Is Put Together 172 A Lesson Learned 176

Description:
Write powerful Ruby code that is easy to maintain and change. With metaprogramming, you can produce elegant, clean, and beautiful programs. Once the domain of expert Rubyists, metaprogramming is now accessible to programmers of all levels. This thoroughly revised and updated second edition of the be
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.