Practical Metaprogramming (Ruby)

Cd971a115964e5570ef5bf2f27b82e7f?s=47 Steven Harms
October 01, 2011

Practical Metaprogramming (Ruby)

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!

Cd971a115964e5570ef5bf2f27b82e7f?s=128

Steven Harms

October 01, 2011
Tweet

Transcript

  1. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Practical Metaprogramming Steven G. Harms 2011-09-30 Steven G. Harms Practical Metaprogramming
  2. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Contact Me! Steven G. Harms Physically: San Francisco, CA Email: rubyconfXI@sgharms.oib.com Twitter / GitHub: sgharms G+ Steven G. Harms Practical Metaprogramming
  3. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Nawlins Be sure to visit the Roosevelt Steven G. Harms Practical Metaprogramming
  4. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience What We’ll Cover Practical Metaprogramming Steven G. Harms Practical Metaprogramming
  5. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Im-practical Metaprogramming Steven G. Harms Practical Metaprogramming
  6. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Im-practical Metaprogramming Episode 012: Metaprogramming Steven G. Harms Practical Metaprogramming
  7. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  8. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  9. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  10. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  11. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Huh? Steven G. Harms Practical Metaprogramming
  12. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Im-practical Metaprogramming: Metaprogramming un-defined Working definitions proposed: “Code that writes code” Steven G. Harms Practical Metaprogramming
  13. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  14. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  15. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  16. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  17. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  18. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Practical Metaprogramming Steven G. Harms Practical Metaprogramming
  19. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Practical Metaprogramming Written by a Ruby Average JoeTM, Steven Harms, not Ruby-legend Steven G. Harms Practical Metaprogramming
  20. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  21. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  22. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Why You Need To Learn This: Precedent 1 Virtually all core libraries make use of MP Steven G. Harms Practical Metaprogramming
  23. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Why You Need To Learn This: Precedent 1 Virtually all core libraries make use of MP Steven G. Harms Practical Metaprogramming
  24. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  25. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Why You Need To Learn This: Your Future 1 Save yourself a lot of typing Steven G. Harms Practical Metaprogramming
  26. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  27. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  28. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Goals 1 Dispel FUD around metaprogramming: you should metaprogram boldly – I want you to feel awesome! Steven G. Harms Practical Metaprogramming
  29. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  30. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  31. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  32. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  33. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  34. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    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
  35. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Intermission INTERMISSION Steven G. Harms Practical Metaprogramming
  36. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience Socially Awkward Penguin Steven G. Harms Practical Metaprogramming
  37. Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To

    You Goals Audience End Slide if Everyone Leaves Forever Alone. . . Steven G. Harms Practical Metaprogramming
  38. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  39. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP So, seriously, what is metaprogramming? Steven G. Harms Practical Metaprogramming
  40. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  41. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  42. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  43. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  44. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Start simply Steven G. Harms Practical Metaprogramming
  45. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Why did this fail? Steven G. Harms Practical Metaprogramming
  46. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Why did this fail? Because a message was passed that Ruby did not know about Steven G. Harms Practical Metaprogramming
  47. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Why did this fail? Because a message was passed that Ruby did not know about Steven G. Harms Practical Metaprogramming
  48. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP “How can we re-direct this message?” Steven G. Harms Practical Metaprogramming
  49. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP “How can we re-direct this message?” def Steven G. Harms Practical Metaprogramming
  50. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP “How can we re-direct this message?” def Steven G. Harms Practical Metaprogramming
  51. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Put the defs in a class Steven G. Harms Practical Metaprogramming
  52. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Put the defs in a module Steven G. Harms Practical Metaprogramming
  53. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Put the defs in the module into the class (mixin) Steven G. Harms Practical Metaprogramming
  54. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Put the defs in the modules into the class (mixin) Steven G. Harms Practical Metaprogramming
  55. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Put the defs in the modules based on random Steven G. Harms Practical Metaprogramming
  56. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Sometimes Ancestors Look Like. . . Frob FrobModTwo FrobMod Object Kernel BasicObject Steven G. Harms Practical Metaprogramming
  57. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP And Sometimes Look Like. . . Frob FrobMod FrobModTwo Object Kernel BasicObject Steven G. Harms Practical Metaprogramming
  58. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  59. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP We were metaprogramming! We were metaprogramming (by my definition) Steven G. Harms Practical Metaprogramming
  60. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  61. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  62. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  63. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  64. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  65. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Metaprogramming Pre-Requisites Basic Method Manipulation Basic Class Definition Basic Module Definition The Mix-In Steven G. Harms Practical Metaprogramming
  66. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Tier 1 Metaprogramming: Advanced Message Redirection Open Class Kernel Method Monkeypatch attr * methods alias :new :old Steven G. Harms Practical Metaprogramming
  67. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Tier 1 Metaprogramming: Open Classes Steven G. Harms Practical Metaprogramming
  68. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Tier 1 Metaprogramming: Kernel Method Steven G. Harms Practical Metaprogramming
  69. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Tier 1 Metaprogramming: Dynamic Getter / Setter Generation Steven G. Harms Practical Metaprogramming
  70. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Tier 1 Metaprogramming: Singleton Method Steven G. Harms Practical Metaprogramming
  71. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP AWESOMENESS – Credit Unknown Steven G. Harms Practical Metaprogramming
  72. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  73. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Thesis: F.U.D. –Tim Connor: SF Ruby Mailing List Steven G. Harms Practical Metaprogramming
  74. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  75. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Teens: Foolishness Steven G. Harms Practical Metaprogramming
  76. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Teens: Brilliance Steven G. Harms Practical Metaprogramming
  77. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  78. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP How Will I Know? Steven G. Harms Practical Metaprogramming
  79. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Explain this: Why are these methods identical structurally? Steven G. Harms Practical Metaprogramming
  80. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  81. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  82. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  83. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  84. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  85. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  86. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Word from Mom and Dad Steven G. Harms Practical Metaprogramming
  87. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    Tier 2 MP Tier 3 MP Word from Mom and Dad Most advanced techniques are fancy syntax for the basic techniques Steven G. Harms Practical Metaprogramming
  88. Introduction Practical MP LatinVerb Demo Supplementary Early Exposure Danger Zone

    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
  89. Introduction Practical MP LatinVerb Demo Supplementary Demo LatinVerb Steven G.

    Harms Practical Metaprogramming
  90. Introduction Practical MP LatinVerb Demo Supplementary Or as Aaron Patterson

    once said: “Do something worthless of questionable value.” – Aaron Patterson Steven G. Harms Practical Metaprogramming
  91. Introduction Practical MP LatinVerb Demo Supplementary Specification Given the four

    principle parts: “am¯ o, am¯ are, am¯ av¯ ı, amatum” Steven G. Harms Practical Metaprogramming
  92. Introduction Practical MP LatinVerb Demo Supplementary Specification Given the four

    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
  93. Introduction Practical MP LatinVerb Demo Supplementary Specification Given the four

    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
  94. Introduction Practical MP LatinVerb Demo Supplementary Tabular View Singular Number

    Plural Number First Person laud¯ o laud¯ amus Second Person laud¯ as laudatis Third Person laudat laudant Steven G. Harms Practical Metaprogramming
  95. Introduction Practical MP LatinVerb Demo Supplementary Model it in Ruby!

    Credit: Allie Brosh Steven G. Harms Practical Metaprogramming
  96. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    means 6 methods to be defined per tense Steven G. Harms Practical Metaprogramming
  97. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    means 6 methods to be defined per tense . . . × 6 tenses (present/imperfect/future/perfect/past- perfect/future-perfect) Steven G. Harms Practical Metaprogramming
  98. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    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
  99. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    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
  100. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    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
  101. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    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
  102. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    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
  103. Introduction Practical MP LatinVerb Demo Supplementary Painful Combinations 6 results

    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
  104. Introduction Practical MP LatinVerb Demo Supplementary Metaprogramming Makes a Saving

    Throw! MP our way out of the pain Credit: Marco26 on DeviantArt Steven G. Harms Practical Metaprogramming
  105. Introduction Practical MP LatinVerb Demo Supplementary Stuff the logic in

    the TenseBlock Class Sub-specify by person (1, 2, 3) or number or cluster by either. Steven G. Harms Practical Metaprogramming
  106. Introduction Practical MP LatinVerb Demo Supplementary Stuff the logic in

    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
  107. Introduction Practical MP LatinVerb Demo Supplementary method missing and dynamic

    dispatch(send) Steven G. Harms Practical Metaprogramming
  108. Introduction Practical MP LatinVerb Demo Supplementary MP Provides: Massive Laziness

    Win ≈ 48 methods covered; 6 written Steven G. Harms Practical Metaprogramming
  109. Introduction Practical MP LatinVerb Demo Supplementary MP Provides: Massive Laziness

    Win ≈ 48 methods covered; 6 written One response class (TenseBlock) Steven G. Harms Practical Metaprogramming
  110. Introduction Practical MP LatinVerb Demo Supplementary Table becomes TenseBlock Singular

    Number Plural Number First Person laud¯ o laud¯ amus Second Person laud¯ as laudatis Third Person laudat laudant = TenseBlock Steven G. Harms Practical Metaprogramming
  111. Introduction Practical MP LatinVerb Demo Supplementary Not Bad Steven G.

    Harms Practical Metaprogramming
  112. Introduction Practical MP LatinVerb Demo Supplementary Scale it Up!: Dynamic

    Dispatch in method missing Be flexible on all 5 aspects Steven G. Harms Practical Metaprogramming
  113. Introduction Practical MP LatinVerb Demo Supplementary Result: Super-Massive Laziness Win

    Covered the thousands of methods predicted Steven G. Harms Practical Metaprogramming
  114. Introduction Practical MP LatinVerb Demo Supplementary Result: Super-Massive Laziness Win

    Covered the thousands of methods predicted . . . and provided the clustering methods as well as a surprising bonus Steven G. Harms Practical Metaprogramming
  115. Introduction Practical MP LatinVerb Demo Supplementary Result: Super-Massive Laziness Win

    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
  116. Introduction Practical MP LatinVerb Demo Supplementary Result: Surprises “Aggregator methods”

    per tense emerged Flexible word order emerged that did the right thing Steven G. Harms Practical Metaprogramming
  117. Introduction Practical MP LatinVerb Demo Supplementary Result: Surprises “Aggregator methods”

    per tense emerged Flexible word order emerged that did the right thing Avoided Java-ish paramteterized brain damage Steven G. Harms Practical Metaprogramming
  118. Introduction Practical MP LatinVerb Demo Supplementary Java-ish Brain Damage: Parameterization

    String calculate vector(VerbyType aV, String v, String m, String t, String p, String n) OR Object[] calculated values = {aV, v, m, t, p, n}; String calculate vector(calculated values); PREFER @aFirst.active voice indicative mood... Steven G. Harms Practical Metaprogramming
  119. Introduction Practical MP LatinVerb Demo Supplementary Usage of Third-Tier 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
  120. Introduction Practical MP LatinVerb Demo Supplementary Listen to your mentors

    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
  121. Introduction Practical MP LatinVerb Demo Supplementary Build beautiful module generators

    from latinverb.rb Steven G. Harms Practical Metaprogramming
  122. Introduction Practical MP LatinVerb Demo Supplementary Build beautiful module generators

    Steven G. Harms Practical Metaprogramming
  123. Introduction Practical MP LatinVerb Demo Supplementary respond to? and ancestors

    Steven G. Harms Practical Metaprogramming
  124. Introduction Practical MP LatinVerb Demo Supplementary respond to? and ancestors

    Steven G. Harms Practical Metaprogramming
  125. Introduction Practical MP LatinVerb Demo Supplementary Greatest Benefit: Clarity &

    Communication “Code that Communicates” Steven G. Harms Practical Metaprogramming
  126. Introduction Practical MP LatinVerb Demo Supplementary Goals Checklist 1 “Metaprogram”

    boldly! Steven G. Harms Practical Metaprogramming
  127. Introduction Practical MP LatinVerb Demo Supplementary Goals Checklist 1 “Metaprogram”

    boldly! Metaprogramming is “just programming” – and you’re already better at it than you know! Steven G. Harms Practical Metaprogramming
  128. Introduction Practical MP LatinVerb Demo Supplementary Goals Checklist 1 “Metaprogram”

    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
  129. Introduction Practical MP LatinVerb Demo Supplementary Goals Checklist 1 “Metaprogram”

    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
  130. Introduction Practical MP LatinVerb Demo Supplementary Goals Checklist 1 “Metaprogram”

    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
  131. Introduction Practical MP LatinVerb Demo Supplementary Goals Checklist 1 “Metaprogram”

    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
  132. Introduction Practical MP LatinVerb Demo Supplementary Goals Checklist 1 “Metaprogram”

    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
  133. Introduction Practical MP LatinVerb Demo Supplementary Supplementary Supplementary Information Steven

    G. Harms Practical Metaprogramming
  134. Introduction Practical MP LatinVerb Demo Supplementary Book Metaprogramming Ruby by

    Paolo Perrotta Steven G. Harms Practical Metaprogramming
  135. Introduction Practical MP LatinVerb Demo Supplementary List of Spells http://ducktypo.blogspot.com/2010/

    08/metaprogramming-spell-book.html Steven G. Harms Practical Metaprogramming
  136. Introduction Practical MP LatinVerb Demo Supplementary (Meta)programming Politely http://confreaks.net/videos/ 374-rubyconf2010-the-polite-programmer-s-guide-to

    -ruby-etiquette Steven G. Harms Practical Metaprogramming
  137. Introduction Practical MP LatinVerb Demo Supplementary Photo Credits 1 “Zen”

    pic: http://www.insidesocal.com/tomhoffarth/archives/2011 /06/shawn-greens-ze.html Steven G. Harms Practical Metaprogramming
  138. Introduction Practical MP LatinVerb Demo Supplementary Colophon L A TEXand

    the Beamer Slide Toolkit Steven G. Harms Practical Metaprogramming
  139. Introduction Practical MP LatinVerb Demo Supplementary SpeakerRate Help Me Get

    Better! http://spkr8.com/t/8534 Steven G. Harms Practical Metaprogramming
  140. Introduction Practical MP LatinVerb Demo Supplementary Work with Me /

    Have Me Teach / Contact Me! Thank You! Steven G. Harms Physically: San Francisco, CA Email: rubyconfXI@sgharms.oib.com Twitter / GitHub: sgharms G+ http://spkr8.com/t/8534 Questions? Steven G. Harms Practical Metaprogramming