Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Introduction Practical MP LatinVerb Demo Supplementary Administration Practicality Benefit To You Goals Audience Contact Me! Steven G. Harms Physically: San Francisco, CA Email: [email protected] Twitter / GitHub: sgharms G+ Steven G. Harms Practical Metaprogramming

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

Introduction Practical MP LatinVerb Demo Supplementary Demo LatinVerb Steven G. Harms Practical Metaprogramming

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

Introduction Practical MP LatinVerb Demo Supplementary Model it in Ruby! Credit: Allie Brosh Steven G. Harms Practical Metaprogramming

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

Introduction Practical MP LatinVerb Demo Supplementary method missing and dynamic dispatch(send) Steven G. Harms Practical Metaprogramming

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

Introduction Practical MP LatinVerb Demo Supplementary Not Bad Steven G. Harms Practical Metaprogramming

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

Introduction Practical MP LatinVerb Demo Supplementary Build beautiful module generators Steven G. Harms Practical Metaprogramming

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

Introduction Practical MP LatinVerb Demo Supplementary Greatest Benefit: Clarity & Communication “Code that Communicates” Steven G. Harms Practical Metaprogramming

Slide 126

Slide 126 text

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

Slide 127

Slide 127 text

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

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

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

Slide 130

Slide 130 text

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

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

Introduction Practical MP LatinVerb Demo Supplementary Supplementary Supplementary Information Steven G. Harms Practical Metaprogramming

Slide 134

Slide 134 text

Introduction Practical MP LatinVerb Demo Supplementary Book Metaprogramming Ruby by Paolo Perrotta Steven G. Harms Practical Metaprogramming

Slide 135

Slide 135 text

Introduction Practical MP LatinVerb Demo Supplementary List of Spells http://ducktypo.blogspot.com/2010/ 08/metaprogramming-spell-book.html Steven G. Harms Practical Metaprogramming

Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

Introduction Practical MP LatinVerb Demo Supplementary Colophon L A TEXand the Beamer Slide Toolkit Steven G. Harms Practical Metaprogramming

Slide 139

Slide 139 text

Introduction Practical MP LatinVerb Demo Supplementary SpeakerRate Help Me Get Better! http://spkr8.com/t/8534 Steven G. Harms Practical Metaprogramming

Slide 140

Slide 140 text

Introduction Practical MP LatinVerb Demo Supplementary Work with Me / 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