Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Make Ruby Write Your Code For You

Make Ruby Write Your Code For You

Despite best intentions, sometimes you just have to write similar code over and over again. This leads to a massive maintenance burden down the road. But, there’s a better way: have the computer write the code for you.

Code generators are shrouded in mystery and can sound unapproachable, but they aren’t! I’ll show you how code generation is no different than web development and why Ruby has all of the tools to build one quickly. You’ll leave this talk knowing where you could apply code generation in your own code base and how to get started!

rambleraptor

November 15, 2018
Tweet

Other Decks in Technology

Transcript

  1. Preface: Mad Libs The _______ ran up the _______. (noun)

    (noun) Then, it ________ jumped ______ the chair. (adverb) (preposition) Nouns: cat, dog, mouse, hill Adverbs: angrily, sadly Preposition: under, around Template Word Bank
  2. Mad Libs End Result The cat ran up the mouse.

    Then, it angrily jumped around the chair. The mouse ran up the dog. Then, it sadly jumped under the chair. The cat ran up the hill. Then, it sadly jumped around the chair. The mouse ran up the cat. Then, it angrily jumped under the chair. The dog ran up the hill. Then, it sadly jumped under the chair. The cat ran up the dog. Then, it sadly jumped around the chair. The cat ran up the mouse. Then, it angrily jumped under the chair. The hill ran up the dog. Then, it angrily jumped around the chair. dot dot dot
  3. Let’s try something more code related a = _____.new() food

    a.add(_________) topping a.order() Template Word Bank Pizza Salad Breadsticks Pepperoni Sausage Lettuce Gummy Bears Bread Sticks API Name (Food name) API Options (Ingredients)
  4. Let’s try something code centric a = Pizza.new() a.add(“sausage”) a.order()

    a = Pizza.new() a.add(“pepperoni”) a.order() a = Breadsticks.new() a.add(“bread”) a.order() a = Breadsticks.new() a.add(“sticks”) a.order() dot dot dot a = Salad.new() a.add(“lettuce”) a.order() a = Salad.new() a.add(“carrots”) a.order()
  5. • No blockchain! • No machine learning! • No compiler

    magic! What auto-generation isn’t (for us)
  6. • Lots of similar-looking code • The overhead of building

    out your templates + framework makes sense • You still might be writing some code manually • Once you start auto-generating, there’s no going back... When do you auto-generate?
  7. • Can define a word bank • Build out some

    human-readable templates • Injects values into templates We need a tool that:
  8. How does this differ from web-dev? • You want as

    much logic in your templates as possible • Templates should be human readable! Don’t be too clever
  9. • Contain everything needed in your templates • Something loopable

    • May have to contain some custom code • Reverse engineer your word bank from your templates Word Bank
  10. • Initializers are never called! • You can add anything

    anywhere! • Potential for bad security issues • TLDR; there are no rules Caveats in YAML
  11. • Initializers are never called! • You can add anything

    anywhere! • Potential for bad security issues • TLDR; there are no rules Caveats in YAML
  12. • Code generation is like Mad Libs • Template that

    looks like code • Word Bank of the things that you’re generating • Runner that combines the two TL;DR