Slide 1

Slide 1 text

Seeing Metaprogramming And Lambda Function Patterns in Ruby Lukas Nimmo [email protected]

Slide 2

Slide 2 text

Geology Minerals

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Ruby is beautiful so our DSLs should be beautiful

Slide 9

Slide 9 text

Implementation Interfaces: The Ruby Way Complexity Public Interface

Slide 10

Slide 10 text

Compare

Slide 11

Slide 11 text

Roadmap Goals/Problems Public Interface Implementation

Slide 12

Slide 12 text

Case #1: Trust Issues

Slide 13

Slide 13 text

Goal Provide configuration options while handling quality assurance

Slide 14

Slide 14 text

Typical Ways of Setting up an Object/Library Object Initialization: One by One:

Slide 15

Slide 15 text

Goal Provide configuration options while handling quality assurance

Slide 16

Slide 16 text

Railsconfig/Config Public Interface

Slide 17

Slide 17 text

Railsconfig’s Implementation: The Block

Slide 18

Slide 18 text

Geocoder Public Interface

Slide 19

Slide 19 text

Geocoder’s Implementation: The Hash

Slide 20

Slide 20 text

Block vs Hash

Slide 21

Slide 21 text

Case #2: Situational Language

Slide 22

Slide 22 text

Goal Offer Situational Language

Slide 23

Slide 23 text

Typical Example of Creating Flexibility One path: Several Paths:

Slide 24

Slide 24 text

Goal Offer Situational Language

Slide 25

Slide 25 text

Hike RSpec Public Interface

Slide 26

Slide 26 text

Hike’s Implementation One path: Several Paths: Condensed Situational Language

Slide 27

Slide 27 text

Common conditions for Situational Language Singular vs. Plural: path and paths

Slide 28

Slide 28 text

Common conditions for Situational Language Synonyms

Slide 29

Slide 29 text

Common conditions for Situational Language Grammatically correct options

Slide 30

Slide 30 text

Con: Cognitive Load

Slide 31

Slide 31 text

Case #3: Leveraging APIs

Slide 32

Slide 32 text

Goal Leveraging generally accepted meaning of existing APIs

Slide 33

Slide 33 text

How Polyglot Might Alter Kernel

Slide 34

Slide 34 text

Public Interface Polyglot

Slide 35

Slide 35 text

Polyglot’s Implementation Still Accessible How it looks now:

Slide 36

Slide 36 text

Problem? Original New

Slide 37

Slide 37 text

Public Interface

Slide 38

Slide 38 text

Case #4: Declarative DSLs

Slide 39

Slide 39 text

Declarative DSL’s for setting up classes in your domain Goal

Slide 40

Slide 40 text

Rails/ActiveRecord

Slide 41

Slide 41 text

Declarative DSL’s for setting up classes in your domain Goal

Slide 42

Slide 42 text

Rails/ActiveRecord Public Interface

Slide 43

Slide 43 text

ActiveRecord’s Implementation

Slide 44

Slide 44 text

Case #5: Domain Switching

Slide 45

Slide 45 text

Define behavior in a familiar domain and execute elsewhere Goal

Slide 46

Slide 46 text

Sinatra Public Interface

Slide 47

Slide 47 text

Define behavior in a familiar domain and execute elsewhere Goal

Slide 48

Slide 48 text

Sinatra’s Implementation

Slide 49

Slide 49 text

Case #6: Opening Doorways

Slide 50

Slide 50 text

Allow many entries around the same structure Goal

Slide 51

Slide 51 text

Clockwork Faraday Accessing Library Structures

Slide 52

Slide 52 text

Allow many entries around the same structure Goal

Slide 53

Slide 53 text

Clockwork Module Instance Class Public Interface

Slide 54

Slide 54 text

Clockwork’s Implementation

Slide 55

Slide 55 text

Faraday Public Interface

Slide 56

Slide 56 text

Faraday’s Implementation

Slide 57

Slide 57 text

Case #7: Dynamic Building

Slide 58

Slide 58 text

Dynamically build objects with the broadest interface possible Goal

Slide 59

Slide 59 text

Typical Object Building

Slide 60

Slide 60 text

Dynamically build objects with the broadest interface possible Goal

Slide 61

Slide 61 text

JBuilder Public Interface

Slide 62

Slide 62 text

JBuilder’s Implementation

Slide 63

Slide 63 text

Paint :my_scheme is now a module Public Interface

Slide 64

Slide 64 text

Paint’s Implementation

Slide 65

Slide 65 text

What is the pattern really?

Slide 66

Slide 66 text

Implementation Interfaces: The Ruby Way Complexity Public Interface

Slide 67

Slide 67 text

Situation Solution Trust Issues Singleton Situational Language alias_method Leveraging Existing APIs Around Alias and Module#prepend Declarative DSLs Class Macros Domain Switching class_eval + Lambdas/Procs/Blocks Opening Doorways Hooks and Method Delegation Dynamic Building method_mising and const_missing

Slide 68

Slide 68 text

Questions? Lukas Nimmo [email protected]