Presentation from Rubyconf XI (New Orleans II). I argue that /all/ ruby development is metaprogramming and that it is not some strange bolt-on to the core language. I hope you will engage with MP boldly for success!
You Goals Audience Contact Me! Steven G. Harms Physically: San Francisco, CA Email: [email protected] Twitter / GitHub: sgharms G+ Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming Episode 012: Metaprogramming 10 minutes into attempting to define “metaprogramming” they had covered. . . Lisp Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming Episode 012: Metaprogramming 10 minutes into attempting to define “metaprogramming” they had covered. . . Lisp Marshaling code, closures, state Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming Episode 012: Metaprogramming 10 minutes into attempting to define “metaprogramming” they had covered. . . Lisp Marshaling code, closures, state Code as data? AST’s versus bytecode Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming Episode 012: Metaprogramming 10 minutes into attempting to define “metaprogramming” they had covered. . . Lisp Marshaling code, closures, state Code as data? AST’s versus bytecode Code generation versus runtime behavior changes?!? Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming: Metaprogramming un-defined Working definitions proposed: “Code that writes code” Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming: Metaprogramming un-defined Working definitions proposed: “Code that writes code” “API for dynamic programming within the Ruby language” Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming: Metaprogramming un-defined Working definitions proposed: “Code that writes code” “API for dynamic programming within the Ruby language” “Programming other peoples’ programs” Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming: Metaprogramming un-defined Working definitions proposed: “Code that writes code” “API for dynamic programming within the Ruby language” “Programming other peoples’ programs” “Bowkett: There’s no such thing as MP, there is just programming” Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming: Metaprogramming un-defined Working definitions proposed: “Code that writes code” “API for dynamic programming within the Ruby language” “Programming other peoples’ programs” “Bowkett: There’s no such thing as MP, there is just programming” “Things that treat code as data” Steven G. Harms Practical Metaprogramming
You Goals Audience Im-practical Metaprogramming: Metaprogramming un-defined Working definitions proposed: “Code that writes code” “API for dynamic programming within the Ruby language” “Programming other peoples’ programs” “Bowkett: There’s no such thing as MP, there is just programming” “Things that treat code as data” “Anything that would blow the mind of a Java programmer.” Steven G. Harms Practical Metaprogramming
You Goals Audience Practical Metaprogramming Written by a Ruby Average JoeTM, Steven Harms, not Ruby-legend Focuses on relatable experiences, not theory Steven G. Harms Practical Metaprogramming
You Goals Audience Practical Metaprogramming Written by a Ruby Average JoeTM, Steven Harms, not Ruby-legend Focuses on relatable experiences, not theory Builds from basic Ruby Steven G. Harms Practical Metaprogramming
You Goals Audience Why You Need To Learn This: Precedent 1 Virtually all core libraries make use of MP 2 Rails uses MP all over the place Steven G. Harms Practical Metaprogramming
You Goals Audience Why You Need To Learn This: Your Future 1 Save yourself a lot of typing 2 Reflect the interior world of your problem domain (including ambiguity!) in your application code Steven G. Harms Practical Metaprogramming
You Goals Audience Why You Need To Learn This: Your Future 1 Save yourself a lot of typing 2 Reflect the interior world of your problem domain (including ambiguity!) in your application code 3 Pleasant surprises Steven G. Harms Practical Metaprogramming
You Goals Audience Goals 1 Dispel FUD around metaprogramming: you should metaprogram boldly – I want you to feel awesome! Steven G. Harms Practical Metaprogramming
You Goals Audience Goals 1 Dispel FUD around metaprogramming: you should metaprogram boldly – I want you to feel awesome! Demonstrate that metaprogramming is “just programming” Steven G. Harms Practical Metaprogramming
You Goals Audience Goals 1 Dispel FUD around metaprogramming: you should metaprogram boldly – I want you to feel awesome! Demonstrate that metaprogramming is “just programming” Describe four “tiers” of metaprogramming spells so you know how to “level up” Steven G. Harms Practical Metaprogramming
You Goals Audience Goals 1 Dispel FUD around metaprogramming: you should metaprogram boldly – I want you to feel awesome! Demonstrate that metaprogramming is “just programming” Describe four “tiers” of metaprogramming spells so you know how to “level up” 2 Let the ancestor chain guide us (“Gray’s Mandate”) Steven G. Harms Practical Metaprogramming
You Goals Audience Goals 1 Dispel FUD around metaprogramming: you should metaprogram boldly – I want you to feel awesome! Demonstrate that metaprogramming is “just programming” Describe four “tiers” of metaprogramming spells so you know how to “level up” 2 Let the ancestor chain guide us (“Gray’s Mandate”) 3 Show a real-world example of the benefits of MP Steven G. Harms Practical Metaprogramming
You Goals Audience Gray’s Mandate Ruby’s method call system is really important. . . and you have to get your head around it at some point about how it does these lookups. . . method call system is a straight line with stops along the way. . . once you get the hang of it. . . you realize you can put a module at any point on that line. – James Edward Gray II (@JEGII), Ruby Rogues ep. 012, ≈ 23m Steven G. Harms Practical Metaprogramming
You Goals Audience Audience Beginner / Intermediate Rubyists will benefit from the first part of this talk. Intermediate/Advanced Rubyists will benefit from the “metaprogramming in the wild” demonstration that uses my library LatinVerb Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP “Practical Metaprogramming: First Contact” . . . and is it to be called an “eigenclass” or a “singleton class,” ma’am? Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP So, seriously, what is metaprogramming? “Writing code that re-directs passed messages at runtime or that provides or alters the structures that do said passing.” Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP So, seriously, what is metaprogramming? “Writing code that re-directs passed messages at runtime or that provides or alters the structures that do said passing.” –Steven Harms Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP But wait. . . isn’t that just regular programming? Yes. Exactly. Ergo: “There is no metaprogramming – just programming” Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP But wait. . . isn’t that just regular programming? Yes. Exactly. Ergo: “There is no metaprogramming – just programming” Let’s see it in action Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Quod Erat Demonstrandum. . . With: Ancestor chain Modules and Classes Catching, intercepting, and re-routing messages based on conditions (e.g. rand) occurs. We “wrote code that re-directed passed messages at runtime or that provided or altered the structures that do said processing.” Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP We were metaprogramming! We were metaprogramming (by my definition) We used only techniques from Ruby basics tutorials (e.g. def, module, class) but reacted and re-directed based on runtime logic ( >rand) Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP We were metaprogramming! We were metaprogramming (by my definition) We used only techniques from Ruby basics tutorials (e.g. def, module, class) but reacted and re-directed based on runtime logic ( >rand) Therefore: Either metaprogramming is much easier than we thought, OR Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP We were metaprogramming! We were metaprogramming (by my definition) We used only techniques from Ruby basics tutorials (e.g. def, module, class) but reacted and re-directed based on runtime logic ( >rand) Therefore: Either metaprogramming is much easier than we thought, OR The difference between “programming” and “metaprogramming” is unimportant Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP That Said. . . It is convenient to refer to metaprogramming as a style of programming When the language has a clear function to do X, don’t write a MP re-implementation without good reason Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Momentary Aside: Terminology “Spells” and their names derive from Metaprogramming Ruby by Paolo “Nusco” Perrotta: http://ducktypo.blogspot.com/2010/08/ metaprogramming-spell-book.html Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP . . . Or Madness? Incautiously used, these lead to the dangers of MP: Opaqueness Unpredictability Unsupportability Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Antithesis: anti-F.U.D. –Paolo Perrotta, author of Metaprogramming Ruby, in e-mail to Steven Harms Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Synthesis: Good Parenting, or Alloparenting “. . . if root’s strong, tree survive” – Mr. Miyagi Singleton Module (“Eigenclass”) Module#ancestors Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Tier 2 Metaprogramming Idioms Generally, methods for the interception and interpretation of passed messages as handled by you! Hybrids involving earlier techniques Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Tier 2 Metaprogramming Idioms Generally, methods for the interception and interpretation of passed messages as handled by you! Hybrids involving earlier techniques Dynamic Dispatch: Object#send Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Tier 2 Metaprogramming Idioms Generally, methods for the interception and interpretation of passed messages as handled by you! Hybrids involving earlier techniques Dynamic Dispatch: Object#send Method Missing: use method missing(sym,args) Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Tier 2 Metaprogramming Idioms Generally, methods for the interception and interpretation of passed messages as handled by you! Hybrids involving earlier techniques Dynamic Dispatch: Object#send Method Missing: use method missing(sym,args) Ghost Method: call methods on a BasicObject to trigger method missing Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Tier 2 Metaprogramming Idioms Generally, methods for the interception and interpretation of passed messages as handled by you! Hybrids involving earlier techniques Dynamic Dispatch: Object#send Method Missing: use method missing(sym,args) Ghost Method: call methods on a BasicObject to trigger method missing Around Alias: Monkeypatch / Open Class a method call and keep a reference to the original Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Tier 3 Metaprogramming Dynamic Generation and Inclusion of Modules Namespace: Dynamically generate Class Extension: Dynamically include No-holds-barred manipulation of classes and instances class eval instance eval Steven G. Harms Practical Metaprogramming
Tier 2 MP Tier 3 MP Word from Mom and Dad Most advanced techniques are fancy syntax for the basic techniques Uncle Ben’s Axiom: With great power comes great responsibility Steven G. Harms Practical Metaprogramming
principle parts: “am¯ o, am¯ are, am¯ av¯ ı, amatum” Respond with a unique value (“conjugation”), am¯ o, to a call with the parameters: “(active voice), (indicative mood), (present tense), (first person), (singular number)” (Fully-Qualified) Steven G. Harms Practical Metaprogramming
principle parts: “am¯ o, am¯ are, am¯ av¯ ı, amatum” Respond with a unique value (“conjugation”), am¯ o, to a call with the parameters: “(active voice), (indicative mood), (present tense), (first person), (singular number)” (Fully-Qualified) Flexibly respond to calls that lack some specification datum e.g. leave out the “number” parameter, receive with less-granular return values e.g. “active voice indicative mood present tense” (3 aspects provided, 6 results) Steven G. Harms Practical Metaprogramming
means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) Steven G. Harms Practical Metaprogramming
means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) . . . × 2 voices (active/present) Steven G. Harms Practical Metaprogramming
means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) . . . × 2 voices (active/present) . . . and then there’s another mood with 4 tenses of its own! Steven G. Harms Practical Metaprogramming
means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) . . . × 2 voices (active/present) . . . and then there’s another mood with 4 tenses of its own! Each regular Latin verb has ≈ 160 unique vectors Steven G. Harms Practical Metaprogramming
means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) . . . × 2 voices (active/present) . . . and then there’s another mood with 4 tenses of its own! Each regular Latin verb has ≈ 160 unique vectors There are 5 standard paradigms Steven G. Harms Practical Metaprogramming
means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) . . . × 2 voices (active/present) . . . and then there’s another mood with 4 tenses of its own! Each regular Latin verb has ≈ 160 unique vectors There are 5 standard paradigms . . . and at least 1, 000 verbs Steven G. Harms Practical Metaprogramming
means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) . . . × 2 voices (active/present) . . . and then there’s another mood with 4 tenses of its own! Each regular Latin verb has ≈ 160 unique vectors There are 5 standard paradigms . . . and at least 1, 000 verbs Possibly thousands of methods or decision flows in a language with poor MP Steven G. Harms Practical Metaprogramming
the TenseBlock Class Sub-specify by person (1, 2, 3) or number or cluster by either. . . . and allow terms in method call to be reordered Steven G. Harms Practical Metaprogramming
Number Plural Number First Person laud¯ o laud¯ amus Second Person laud¯ as laudatis Third Person laudat laudant = TenseBlock Steven G. Harms Practical Metaprogramming
Covered the thousands of methods predicted . . . and provided the clustering methods as well as a surprising bonus Steven G. Harms Practical Metaprogramming
Covered the thousands of methods predicted . . . and provided the clustering methods as well as a surprising bonus I only wrote 24 methods Steven G. Harms Practical Metaprogramming
per tense emerged Flexible word order emerged that did the right thing Avoided Java-ish paramteterized brain damage Steven G. Harms Practical Metaprogramming
in LatinVerb DSL: See Evan’s talk from Lone Star Ruby Conf Class Extension a.k.a. Mixin Module namespace cleanliness Steven G. Harms Practical Metaprogramming
David Brady: “Use modules, kids.” JEG2: “Understand your ancestor chain” Jim Weirich: “Polite programmers respond to? with true for their metaprogrammed methods” Corey Haines: “Extraction into modules allows you to build super-fast tests! (GoGaRuCo 2011)” Steven G. Harms Practical Metaprogramming
boldly! Metaprogramming is “just programming” – and you’re already better at it than you know! Study and experiment with the four “tiers” of metaprogramming spells Steven G. Harms Practical Metaprogramming
boldly! Metaprogramming is “just programming” – and you’re already better at it than you know! Study and experiment with the four “tiers” of metaprogramming spells Remain humble and continue to listen for guidance Steven G. Harms Practical Metaprogramming
boldly! Metaprogramming is “just programming” – and you’re already better at it than you know! Study and experiment with the four “tiers” of metaprogramming spells Remain humble and continue to listen for guidance 2 Use the ancestor chain as your guide – Answer Gray’s mandate boldly! Steven G. Harms Practical Metaprogramming
boldly! Metaprogramming is “just programming” – and you’re already better at it than you know! Study and experiment with the four “tiers” of metaprogramming spells Remain humble and continue to listen for guidance 2 Use the ancestor chain as your guide – Answer Gray’s mandate boldly! 3 Let MP surprise you, model ambiguity cleanly, and help communicate your domain Steven G. Harms Practical Metaprogramming
boldly! Metaprogramming is “just programming” – and you’re already better at it than you know! Study and experiment with the four “tiers” of metaprogramming spells Remain humble and continue to listen for guidance 2 Use the ancestor chain as your guide – Answer Gray’s mandate boldly! 3 Let MP surprise you, model ambiguity cleanly, and help communicate your domain 4 Thank the Ruby Rogues for taking a bit of a ribbing Steven G. Harms Practical Metaprogramming
Have Me Teach / Contact Me! Thank You! Steven G. Harms Physically: San Francisco, CA Email: [email protected] Twitter / GitHub: sgharms G+ http://spkr8.com/t/8534 Questions? Steven G. Harms Practical Metaprogramming