Slide 1

Slide 1 text

Make Ruby Write Your Code For You Alex Stephen @rambleraptor

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

github.com/googlecloudplatform/ magic-modules

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Auto-generation: Computer writes code for you

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Auto-generation: You tell the computer what to write

Slide 8

Slide 8 text

Auto-generation: You tell the computer how to write it

Slide 9

Slide 9 text

Auto-generation: The computer writes what you want where you want it

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Auto-generation: “Write once, write anywhere****”

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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()

Slide 17

Slide 17 text

Auto-generation is just like Mad Libs!

Slide 18

Slide 18 text

● No blockchain! ● No machine learning! ● No compiler magic! What auto-generation isn’t (for us)

Slide 19

Slide 19 text

When should I use this?

Slide 20

Slide 20 text

Abstractions

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Functions provide code reuse

Slide 25

Slide 25 text

Functions provide abstraction over functionality actions

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Classes provide abstraction over things

Slide 28

Slide 28 text

Classes provide abstraction over inputs

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

We don’t have a way to fix the copy-paste problem

Slide 33

Slide 33 text

We don’t have a way to abstract for similar functionality

Slide 34

Slide 34 text

We don’t have a way to abstract for similar functionality at scale

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Adding new features by hand takes roughly the same amount of time (except tech debt)

Slide 37

Slide 37 text

Adding new features by auto-generation takes tons of time at first, but eventually it pays off

Slide 38

Slide 38 text

● 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?

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Ruby!

Slide 41

Slide 41 text

● Can define a word bank ● Build out some human-readable templates ● Injects values into templates We need a tool that:

Slide 42

Slide 42 text

Templates

Slide 43

Slide 43 text

ERB! require ‘erb’

Slide 44

Slide 44 text

ERB! require ‘erb’

Slide 45

Slide 45 text

What if my template looked like Ruby code?

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Word Bank

Slide 50

Slide 50 text

● 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

Slide 51

Slide 51 text

YAML! YAML.load()

Slide 52

Slide 52 text

YAML! YAML.load()

Slide 53

Slide 53 text

YAML! YAML.load()

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

● 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

Slide 58

Slide 58 text

Thank you! twitter.com/rambleraptor github.com/rambleraptor speakerdeck.com/rambleraptor github.com/googlecloudplatform/magic-modules