Metaprogramming Ruby program like the Ruby pros 1st Edition Paolo Perrotta 2024 Scribd Download
Metaprogramming Ruby program like the Ruby pros 1st Edition Paolo Perrotta 2024 Scribd Download
com
https://ebookname.com/product/metaprogramming-ruby-program-
like-the-ruby-pros-1st-edition-paolo-perrotta/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/the-ruby-way-solutions-and-techniques-
in-ruby-programming-second-edition-desconocido/
ebookname.com
https://ebookname.com/product/ruby-for-rails-ruby-techniques-for-
rails-developers-david-black/
ebookname.com
https://ebookname.com/product/ruby-pocket-reference-2nd-edition-
instant-help-for-ruby-programmers-michael-fitzgerald/
ebookname.com
https://ebookname.com/product/fulke-greville-lord-
brooke-1554-1628-joan-rees/
ebookname.com
Sticky Faith Youth Worker Edition Practical Ideas to
Nurture Long Term Faith in Teenagers Kara Powell & Brad M.
Griffin & Cheryl A. Crawford
https://ebookname.com/product/sticky-faith-youth-worker-edition-
practical-ideas-to-nurture-long-term-faith-in-teenagers-kara-powell-
brad-m-griffin-cheryl-a-crawford/
ebookname.com
https://ebookname.com/product/pygmalion-webster-s-thesaurus-edition-
george-bernard-shaw/
ebookname.com
https://ebookname.com/product/radiowave-propagation-and-antennas-for-
personal-communications-3rd-edition-kazimierz-siwiak/
ebookname.com
https://ebookname.com/product/the-complete-idiot-s-guide-to-
persuasion-1st-edition-ed-d/
ebookname.com
Mathematical Physics Applied Mathematics for Scientists
and Engineers Second Edition Bruce R. Kusse
https://ebookname.com/product/mathematical-physics-applied-
mathematics-for-scientists-and-engineers-second-edition-bruce-r-kusse/
ebookname.com
What Readers Are Saying About Metaprogramming Ruby
Reading this book was like diving into a new world of thinking. I tried
a mix of Java and JRuby metaprogramming on a recent project. Using
Java alone would now feel like entering a sword fight carrying only
a banana, when my opponent is wielding a one-meter-long Samurai
blade.
Sebastian Hennebrüder
Java Consultant and Trainer, laliluna.de
This Ruby book fills a gap between language reference manuals and
programming cookbooks. Not only does it explain various meta-
programming facilities, but it also shows a pragmatic way of making
software smaller and better. There’s a caveat, though; when the new
knowledge sinks in, programming in more mainstream languages will
start feeling like a chore.
Jurek Husakowski
Software Designer, Philips Applied Technologies
Before this book, I’d never found a clear organization and explanation
of concepts like the Ruby object model, closures, DSLs definition, and
eigenclasses all spiced with real-life examples taken from the gems we
usually use every day. This book is definitely worth reading.
Carlo Pecchia
Software Engineer
I’ve had a lot of trouble finding a good way to pick up these meta-
programming techniques, and this book is bar none the best way to
do it. Paolo Perrotta makes it painless to learn Ruby’s most complex
secrets and use them in practical applications.
Chris Bunch
Software Engineer
Metaprogramming Ruby
Program Like the Ruby Pros
Paolo Perrotta
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://www.pragprog.com
ISBN-10: 1-934356-47-6
ISBN-13: 978-1-934356-47-0
Printed on acid-free paper.
P1.0 printing, January 2010
Version: 2010-1-29
To Carlo.
Contents
Foreword 10
Acknowledgments 11
Introduction 13
The “M” Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . 21
About You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
I Metaprogramming Ruby 26
2 Tuesday: Methods 60
2.1 A Duplication Problem . . . . . . . . . . . . . . . . . . . 61
2.2 Dynamic Methods . . . . . . . . . . . . . . . . . . . . . . 63
2.3 method_missing() . . . . . . . . . . . . . . . . . . . . . . 71
2.4 Quiz: Bug Hunt . . . . . . . . . . . . . . . . . . . . . . . 82
2.5 More method_missing() . . . . . . . . . . . . . . . . . . . 84
3 Wednesday: Blocks 91
3.1 How to Handle Hump Day . . . . . . . . . . . . . . . . . 92
3.2 Quiz: Ruby# . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.3 Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.4 instance_eval() . . . . . . . . . . . . . . . . . . . . . . . . 105
CONTENTS 8
6 Epilogue 188
D Bibliography 268
Index 269
Foreword
Ruby inherits characteristics from various languages — Lisp, Small-
talk, C, and Perl, to name a few. Metaprogramming comes from Lisp
(and Smalltalk). It’s a bit like magic, which makes something astonish-
ing 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
October 2009
Acknowledgments
Before I begin, I need to thank a few people. I’m talking to you, gen-
tlemen: Joe Armstrong, Satoshi Asakawa, Paul Barry, Emmanuel Ber-
nard, Roberto Bettazzoni, Ola Bini, Piergiuliano Bossi, Simone Busoli,
Andrea Cisternino, Davide D’Alto, Mauro Di Nuzzo, Marco Di Timo-
teo, Mauricio Fernandez, Jay Fields, Michele Finelli, Neal Ford, Flo-
rian Frank, Sanne Grinovero, Federico Gobbo, Florian Groß, Sebastian
Hennebrüder, Doug Hudson, Jurek Husakowski, Lyle Johnson, Luca
Marchetti, MenTaLguY, Carlo Pecchia, Andrea Provaglio, Mike Roberts,
Martin Rodgers, Jeremy Sydik, Andrea Tomasini, Marco Trincardi, Ivan
Vaghi, Giancarlo Valente, Davide Varvello, Jim Weirich, and the dozens
of readers who reported problems and errata while this book was in
beta. Whether you provided reviews, quotes, fixes, opinions, or moral
support, there’s at least one line in this book that changed for the bet-
ter because of you. Did I say one line? For some of you, make that “a
few chapters.” In particular, Ola, Satoshi, and Jurek deserve a special
place on this page and my enduring gratitude.
Thanks to the staff at the Pragmatic Bookshelf: Janet Furlow, Seth
Maislin, Steve Peter, Susannah Davidson Pfalzer, and Kim Wimpsett.
Dave and Andy, thank you for believing in this project when times got
rough. Jill, thank you for making my awkward prose look so effortless.
Our crunch week in Venice was a lot of hard work, but it was definitely
worth it. And speaking of Venice: thank you, Lucio, for being such a
dear old friend.
Mom and Dad, thank you for your support, for your love, and for never
asking why I was taking so long to finish this book.
Most authors’ closing thanks go to their partners, and now I know why.
When you’re about to finish a book, you turn back to the day when
you started writing, and it feels so far away. I remember writing over
lunch breaks, nights, and weekends, locked for days or weeks inside
A CKNOWLEDGMENTS 12
Introduction
Metaprogramming. . . it sounds cool! It sounds like a design technique
for high-level enterprise architects or a fashionable buzzword that has
found its way into press releases.
In fact, far from being an abstract concept or a bit of marketing-speak,
metaprogramming is a collection of down-to-earth, pragmatic coding
techniques. It doesn’t just sound cool; it is cool. Here are some of the
things you can do with metaprogramming in the Ruby language:
• Say you want to write a Ruby program that connects to an external
system—maybe a web service or a Java program. With metapro-
gramming, you can write a wrapper that takes any method call
and routes it to the external system. If somebody adds methods
to the external system later, you don’t have to change your Ruby
wrapper; the wrapper will support the new methods right away.
That’s magic!
• Maybe you have a problem that would be best solved with a pro-
gramming language that’s specific to that problem. You could go
to the trouble of writing your own language, custom parser and
all. Or you could just use Ruby, bending its syntax until it looks
like a specific language for your problem. You can even write your
own little interpreter that reads code written in your Ruby-based
language from a file.
• You can remove duplication from your Ruby program at a level
that Java programmers can only dream of. Let’s say you have
twenty methods in a class, and they all look the same. How about
defining all those methods at once, with just a few lines of code?
Or maybe you want to call a sequence of similarly named meth-
ods. How would you like a single short line of code that calls all
the methods whose names match a pattern—like, say, all methods
that begin with test?
T HE “M” W ORD 14
• You can stretch and twist Ruby to meet your needs, rather than
adapt to the language as it is. For example, you can enhance any
class (even a core class like Array) with that method you miss so
dearly, you can wrap logging functionality around a method that
you want to monitor, you can execute custom code whenever a
client inherits from your favorite class. . . the list goes on. You are
limited only by your own, undoubtedly fertile, imagination.
Metaprogramming gives you the power to do all these things. Let’s see
what it looks like.
Introspection
Take a look at this code:
Download introduction/introspection.rb
class Greeting
def initialize(text)
@text = text
end
def welcome
@text
end
end
my_object = Greeting.new("Hello" )
I defined a Greeting class and created a Greeting object. I can now turn
to the language constructs and ask them questions.
Exploring the Variety of Random
Documents with Different Content
ACANTHORHYNCHUS TENUIROSTRIS.
Slender-billed Spine-bill.
J. Gould
and H. C.
Richter del
et lith.
C.
Hullmandel
Imp.
ACANTHORHYNCHUS SUPERCILIOSUS,
Gould.
White-eyebrowed Spine-bill.
J. Gould
and H. C.
Richter del
et lith.
C.
Hullmandel
Imp.
MYZOMELA SANGUINOLENTA.
Sanguineous Honey-eater.
J. Gould
and H. C.
Richter del
et lith.
C.
Hullmandel
Imp.
MYZOMELA ERYTHROCEPHALA, Gould.
Red-headed Honey-eater.
J. Gould
and H. C.
Richter del
et lith.
C.
Hullmandel
Imp.
MYZOMELA PECTORALIS, Gould.
Banded Honey-eater.
Drawn
from
Nature &
on Stone by
J. & E.
Gould.
Printed by
C.
Hullmandel
.
MYZOMELA NIGRA, Gould.
Black Honey-eater.
J. Gould
and H. C.
Richter del
et lith.
Hullmandel
& Walton
Imp.
MYZOMELA OBSCURA, Gould.
Obscure Honey-eater.
J. & E.
Gould del et
lith.
C.
Hullmandel
Imp.
ENTOMYZA CYANOTIS, Swains.
Blue-faced Entomyza.
J. Gould
and H. C.
Richter del
et lith.
C.
Hullmandel
Imp.
ENTOMYZA ALBIPENNIS, Gould.
White-pinioned Honey-eater.
J. Gould
and H. C.
Richter del
et lith.
Hullmandel
& Walton
Imp.
MELITHREPTUS VALIDIROSTRIS, Gould.
Strong-billed Honey-eater.