Slide 1

Slide 1 text

OPENSTRUCT BUILDING A BETTER ARIEL CAPLAN

Slide 2

Slide 2 text

OPENSTRUCT LET’S TALK ABOUT

Slide 3

Slide 3 text

OPENSTRUCT IS RUBY’S JAVASCRIPT OBJECT

Slide 4

Slide 4 text

OPENSTRUCT IS RUBY’S JAVASCRIPT OBJECT require 'ostruct' open_struct = OpenStruct.new(foo: :bar) open_struct.baz = 4 open_struct[:something] = 'whatever'

Slide 5

Slide 5 text

OPENSTRUCT IS RUBY’S JAVASCRIPT OBJECT require 'ostruct' open_struct = OpenStruct.new(foo: :bar) open_struct.baz = 4 open_struct[:something] = 'whatever' open_struct.foo => :bar open_struct.baz => 4 open_struct.quux => nil

Slide 6

Slide 6 text

OPENSTRUCT IS RUBY’S JAVASCRIPT OBJECT require 'ostruct' open_struct = OpenStruct.new(foo: :bar) open_struct.baz = 4 open_struct[:something] = 'whatever' open_struct[:foo] => :bar open_struct["baz"] => 4 open_struct[:quux] => nil open_struct.foo => :bar open_struct.baz => 4 open_struct.quux => nil

Slide 7

Slide 7 text

WHY USE OPENSTRUCT? 3 common use cases for OpenStruct (Erik Michaels-Ober's list): • Consume an API • Configuration object • Simple test double

Slide 8

Slide 8 text

CONSUME AN API

Slide 9

Slide 9 text

CONSUME AN API hash = JSON.parse(APICall.execute) results = OpenStruct.new(hash) SIMPLE STRATEGY: PASS RESPONSE INTO AN OPENSTRUCT

Slide 10

Slide 10 text

CONSUME AN API hash = JSON.parse(APICall.execute) results = OpenStruct.new(hash) class APICall < OpenStruct def self.execute response = Net::HTTP.get(URI('domain.com/endpoint')) new(JSON.parse(response)) end end results = APICall.execute SIMPLE STRATEGY: PASS RESPONSE INTO AN OPENSTRUCT ADVANCED STRATEGY: SUBCLASS OPENSTRUCT

Slide 11

Slide 11 text

CONSUME AN API { "thing": [1, 2, 3], "other_thing": "hello" } If the API response is…

Slide 12

Slide 12 text

CONSUME AN API { "thing": [1, 2, 3], "other_thing": "hello" } If the API response is… Instead of… response['thing'] => [1, 2, 3]

Slide 13

Slide 13 text

CONSUME AN API { "thing": [1, 2, 3], "other_thing": "hello" } If the API response is… response.thing => [1, 2, 3] You can write this instead! Instead of… response['thing'] => [1, 2, 3]

Slide 14

Slide 14 text

CONSUME AN API You can also define your own methods! class User < OpenStruct def name "#{first_name} #{last_name}" end end api_call = JSON.parse(API.get('/users/1')) # => {"first_name"=>"Ariel","last_name"=>"Caplan"} User.new(api_call).name # => "Ariel Caplan"

Slide 15

Slide 15 text

CONFIGURATION OBJECT

Slide 16

Slide 16 text

CONFIGURATION OBJECT MyGem.configure do |configuration| configuration.setting = true end

Slide 17

Slide 17 text

CONFIGURATION OBJECT class MyGem def self.configure yield configuration end def self.configuration @configuration ||= OpenStruct.new end end MyGem.configure do |configuration| configuration.setting = true end

Slide 18

Slide 18 text

TEST DOUBLE class Order def initialize(payment_gateway, products) @payment_gateway = payment_gateway @products = products end def pay payment_gateway.total = total_cost payment_gateway.charge end def total_cost # sum up the products end end

Slide 19

Slide 19 text

TEST DOUBLE class Order def initialize(payment_gateway, products) @payment_gateway = payment_gateway @products = products end def pay payment_gateway.total = total_cost payment_gateway.charge end def total_cost # sum up the products end end Expensive to test!

Slide 20

Slide 20 text

TEST DOUBLE # Stub out methods payment_gateway = OpenStruct.new(charge: :PAID) # Run the test payment_result = Order.new(payment_gateway, products).pay # Assert a value is returned assert_equal payment_result, :PAID # Assert a value is set on the OpenStruct assert_equal payment_gateway.total, total_cost(products)

Slide 21

Slide 21 text

HOW DOES IT WORK?

Slide 22

Slide 22 text

WARNING! CODE HAS BEEN BRUTALLY EDITED

Slide 23

Slide 23 text

HOW DOES IT WORK?

Slide 24

Slide 24 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class.

Slide 25

Slide 25 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. foo = Object.new def foo.bar "hello from bar" end foo.bar => "hello from bar" foo.singleton_methods => [:bar]

Slide 26

Slide 26 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class.

Slide 27

Slide 27 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. OpenStruct.new(foo: :bar)

Slide 28

Slide 28 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def initialize(hash=nil) @table = {} if hash hash.each_pair do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end OpenStruct.new(foo: :bar)

Slide 29

Slide 29 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def initialize(hash=nil) @table = {} if hash hash.each_pair do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end OpenStruct.new(foo: :bar) open_struct.baz = 4

Slide 30

Slide 30 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def initialize(hash=nil) @table = {} if hash hash.each_pair do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end OpenStruct.new(foo: :bar) open_struct.baz = 4 open_struct.baz=(4)

Slide 31

Slide 31 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def initialize(hash=nil) @table = {} if hash hash.each_pair do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end def method_missing(mid, *args) mname = mid.id2name if mname.chomp!('=') @table[new_ostruct_member(mname)] = args[0] else @table[mid] end end OpenStruct.new(foo: :bar) open_struct.baz = 4 open_struct.baz=(4)

Slide 32

Slide 32 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def initialize(hash=nil) @table = {} if hash hash.each_pair do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end def method_missing(mid, *args) mname = mid.id2name if mname.chomp!('=') @table[new_ostruct_member(mname)] = args[0] else @table[mid] end end OpenStruct.new(foo: :bar) open_struct.baz = 4 open_struct.baz=(4)

Slide 33

Slide 33 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def initialize(hash=nil) @table = {} if hash hash.each_pair do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end def method_missing(mid, *args) mname = mid.id2name if mname.chomp!('=') @table[new_ostruct_member(mname)] = args[0] else @table[mid] end end OpenStruct.new(foo: :bar) open_struct.baz = 4 open_struct.baz=(4) open_struct.baz

Slide 34

Slide 34 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) define_singleton_method(name) { @table[name] } define_singleton_method("#{name}=") { |x| @table[name] = x } end name end

Slide 35

Slide 35 text

HOW DOES IT WORK? • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) define_singleton_method(name) { @table[name] } define_singleton_method("#{name}=") { |x| @table[name] = x } end name end This is the singleton class version of attr_accessor :foo but it uses the internal @table, not instance variables!

Slide 36

Slide 36 text

• Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. HOW DOES IT WORK?

Slide 37

Slide 37 text

• No 2 OpenStructs share a set of methods • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. HOW DOES IT WORK?

Slide 38

Slide 38 text

• No 2 OpenStructs share a set of methods • The methods must be defined each time! • Under the hood, OpenStruct defines attribute setter/getter methods on the object’s singleton class. HOW DOES IT WORK?

Slide 39

Slide 39 text

OPENSTRUCT IS SLOW

Slide 40

Slide 40 text

• Q: How slow is OpenStruct? OPENSTRUCT IS SLOW

Slide 41

Slide 41 text

• Q: How slow is OpenStruct? • A: OPENSTRUCT IS SLOW

Slide 42

Slide 42 text

• Q: How slow is OpenStruct? • A: OPENSTRUCT IS SLOW • Translation: 10-40x slower than an explicitly defined class

Slide 43

Slide 43 text

• Q: How slow is OpenStruct? • A: OPENSTRUCT IS SLOW • Translation: 10-40x slower than an explicitly defined class • Q: Why is OpenStruct slow?

Slide 44

Slide 44 text

• Q: How slow is OpenStruct? • A: OPENSTRUCT IS SLOW • Translation: 10-40x slower than an explicitly defined class • Q: Why is OpenStruct slow? • Defining methods is slow

Slide 45

Slide 45 text

• Q: How slow is OpenStruct? • A: OPENSTRUCT IS SLOW • Translation: 10-40x slower than an explicitly defined class • Q: Why is OpenStruct slow? • Defining methods is slow • method_missing is slow

Slide 46

Slide 46 text

• Q: How slow is OpenStruct? • A: OPENSTRUCT IS SLOW • Translation: 10-40x slower than an explicitly defined class • Q: Why is OpenStruct slow? • Defining methods is slow • method_missing is slow • In < 2.1, reset the global method cache, no longer a problem

Slide 47

Slide 47 text

http://jamesgolick.com/2013/4/14/
 mris-method-caches.html

Slide 48

Slide 48 text

IN RUBY >= 2.1, WE CAN CREATE OPENSTRUCTS WITHOUT SLOWING DOWN THE REST OF OUR APPLICATION!

Slide 49

Slide 49 text

BUT OPENSTRUCT ITSELF IS STILL SLOW…

Slide 50

Slide 50 text

HI EVERYONE! MY NAME IS ARIEL CAPLAN. YOU CAN FIND ME ONLINE AT @AMCAPLAN AND AMCAPLAN.NINJA

Slide 51

Slide 51 text

I WORK FOR WE PAY PEOPLE TO USE HIGH-QUALITY, LOW-COST HEALTHCARE.

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

WE USE A SYSTEM OF

Slide 54

Slide 54 text

WE USE A SYSTEM OF MICROSERVICES

Slide 55

Slide 55 text

WE USE A SYSTEM OF MICROSERVICES AND

Slide 56

Slide 56 text

WE USE A SYSTEM OF MICROSERVICES AND EXTERNAL APIS

Slide 57

Slide 57 text

WE USE A SYSTEM OF MICROSERVICES AND EXTERNAL APIS COMMUNICATING VIA JSON

Slide 58

Slide 58 text

WE USE A SYSTEM OF MICROSERVICES AND EXTERNAL APIS COMMUNICATING VIA JSON PARSED INTO RUBY HASHES

Slide 59

Slide 59 text

WE USE A SYSTEM OF MICROSERVICES AND EXTERNAL APIS COMMUNICATING VIA JSON PARSED INTO RUBY HASHES FED INTO OPENSTRUCTS

Slide 60

Slide 60 text

PROFILING
 INDICATED THAT OPENSTRUCT WAS A MAJOR CULPRIT

Slide 61

Slide 61 text

WE WANTED TO KEEP OPENSTRUCT BECAUSE IT GAVE US FLEXIBILITY

Slide 62

Slide 62 text

CAN WE BUILD A BETTER OPENSTRUCT?

Slide 63

Slide 63 text

NO.

Slide 64

Slide 64 text

THANK YOU! ARIEL CAPLAN @AMCAPLAN

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

YES.

Slide 67

Slide 67 text

4 STORIES

Slide 68

Slide 68 text

ROUND 1: OpenFastStruct

Slide 69

Slide 69 text

” “ — Ruby Weekly, March 26, 2015

Slide 70

Slide 70 text

” “ — https://github.com/arturoherrero/ofstruct

Slide 71

Slide 71 text

— https://github.com/arturoherrero/ofstruct/blob/master/lib/ofstruct.rb def initialize(args = {}) @members = {} update(args) end def update(args) args.each { |k, v| assign(k, v) } end
 def assign(key, value) @members[key.to_sym] = value end def method_missing(name, *args) @members.fetch(name) do if name[-1] == "=" assign(name[0..-2], args.first) else assign(key, self.class.new) end end end

Slide 72

Slide 72 text

— https://github.com/arturoherrero/ofstruct/blob/master/lib/ofstruct.rb def initialize(args = {}) @members = {} update(args) end def update(args) args.each { |k, v| assign(k, v) } end
 def assign(key, value) @members[key.to_sym] = value end def method_missing(name, *args) @members.fetch(name) do if name[-1] == "=" assign(name[0..-2], args.first) else assign(key, self.class.new) end end end open_fast_struct.baz # baz exists

Slide 73

Slide 73 text

— https://github.com/arturoherrero/ofstruct/blob/master/lib/ofstruct.rb def initialize(args = {}) @members = {} update(args) end def update(args) args.each { |k, v| assign(k, v) } end
 def assign(key, value) @members[key.to_sym] = value end def method_missing(name, *args) @members.fetch(name) do if name[-1] == "=" assign(name[0..-2], args.first) else assign(key, self.class.new) end end end open_fast_struct.baz = 4 open_fast_struct.baz # baz exists

Slide 74

Slide 74 text

— https://github.com/arturoherrero/ofstruct/blob/master/lib/ofstruct.rb def initialize(args = {}) @members = {} update(args) end def update(args) args.each { |k, v| assign(k, v) } end
 def assign(key, value) @members[key.to_sym] = value end def method_missing(name, *args) @members.fetch(name) do if name[-1] == "=" assign(name[0..-2], args.first) else assign(key, self.class.new) end end end open_fast_struct.baz = 4 open_fast_struct.baz # baz does not exist open_fast_struct.baz # baz exists

Slide 75

Slide 75 text

OPENFASTSTRUCT DOESN’T PAY THE UPFRONT COST OF DEFINING METHODS

Slide 76

Slide 76 text

OPENFASTSTRUCT IS GOOD WHEN PROPERTIES ARE NOT ACCESSED REPEATEDLY

Slide 77

Slide 77 text

OPENFASTSTRUCT DOESN’T WORK IN OUR APP DUE TO DIFFERENT BEHAVIOR

Slide 78

Slide 78 text

” “ — https://github.com/arturoherrero/ofstruct

Slide 79

Slide 79 text

” “ — https://github.com/arturoherrero/ofstruct

Slide 80

Slide 80 text

OpenStruct.new.foo => nil OpenFastStruct.new.foo => #

Slide 81

Slide 81 text

OpenStruct.new.foo => nil OpenFastStruct.new.foo => # if open_struct_response.foo

Slide 82

Slide 82 text

OpenStruct.new.foo => nil OpenFastStruct.new.foo => # if open_struct_response.foo “ falsey

Slide 83

Slide 83 text

OpenStruct.new.foo => nil OpenFastStruct.new.foo => # if open_struct_response.foo “ falsey if open_fast_struct_response.foo

Slide 84

Slide 84 text

OpenStruct.new.foo => nil OpenFastStruct.new.foo => # if open_struct_response.foo “ falsey “ truthy if open_fast_struct_response.foo

Slide 85

Slide 85 text

A DROP-IN REPLACEMENT SHOULD BE A DROP-IN REPLACEMENT

Slide 86

Slide 86 text

CAN WE DO BETTER?

Slide 87

Slide 87 text

ROUND 2: PersistentOpenStruct

Slide 88

Slide 88 text

SAMPLE USAGE PersistentOpenStruct

Slide 89

Slide 89 text

SAMPLE USAGE PersistentOpenStruct class Animal < PersistentOpenStruct def speak "The #{type} makes a #{sound} sound!" end end

Slide 90

Slide 90 text

SAMPLE USAGE PersistentOpenStruct class Animal < PersistentOpenStruct def speak "The #{type} makes a #{sound} sound!" end end dog = Animal.new(type: 'dog', sound: 'woof') # => # dog.speak # => "The dog makes a woof sound!" dog.ears = 'droopy' dog[:nose] = ['cold', 'wet'] dog['tail'] = 'waggable' dog # => #

Slide 91

Slide 91 text

SAMPLE USAGE PersistentOpenStruct class Animal < PersistentOpenStruct def speak "The #{type} makes a #{sound} sound!" end end dog = Animal.new(type: 'dog', sound: 'woof') # => # dog.speak # => "The dog makes a woof sound!" dog.ears = 'droopy' dog[:nose] = ['cold', 'wet'] dog['tail'] = 'waggable' dog # => # Animal.instance_methods(false) # => [:speak, :type=, :type, :sound=, :sound, :ears=, :ears,
 :nose=, :nose, :tail=, :tail]

Slide 92

Slide 92 text

HOW IT WORKS PersistentOpenStruct def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) self.class.send(:define_method, name) { @table[name] } self.class.send(:define_method, "#{name}=") { |x| @table[name] = x } end name end def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) define_singleton_method(name) { @table[name] } define_singleton_method("#{name}=") { |x| @table[name] = x } end name end OpenStruct PersistentOpenStruct

Slide 93

Slide 93 text

HOW IT WORKS PersistentOpenStruct def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) self.class.send(:define_method, name) { @table[name] } self.class.send(:define_method, "#{name}=") { |x| @table[name] = x } end name end def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) define_singleton_method(name) { @table[name] } define_singleton_method("#{name}=") { |x| @table[name] = x } end name end OpenStruct PersistentOpenStruct def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) define_singleton_method(name) { @table[name] } define_singleton_method("#{name}=") { |x| @table[name] = x } end name end def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) self.class.send(:define_method, name) { @table[name] } self.class.send(:define_method, "#{name}=") { |x| @table[name] = x } end name end

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

THIS IS A TERRIBLE HACK.

Slide 96

Slide 96 text

THIS IS A TERRIBLE HACK. IT MAY HAVE SECURITY IMPLICATIONS.

Slide 97

Slide 97 text

THIS IS A TERRIBLE HACK. IT MAY HAVE SECURITY IMPLICATIONS. AND IT MADE OUR APP 10% FASTER.

Slide 98

Slide 98 text

Comparison: RegularClass: 3673779.6 i/s PersistentOpenStruct: 764359.4 i/s - 4.81x slower OpenFastStruct: 546808.8 i/s - 6.72x slower OpenStruct: 155130.1 i/s - 23.68x slower

Slide 99

Slide 99 text

WHERE’S THE MATH?

Slide 100

Slide 100 text

THE INFORMATION YOU CRAVE USING THE NOTATION YOU LOVE!

Slide 101

Slide 101 text

THE INFORMATION YOU CRAVE • Let n = number of methods to define USING THE NOTATION YOU LOVE!

Slide 102

Slide 102 text

THE INFORMATION YOU CRAVE • Let n = number of methods to define • Let o = objects to create USING THE NOTATION YOU LOVE!

Slide 103

Slide 103 text

THE INFORMATION YOU CRAVE • Let n = number of methods to define • Let o = objects to create • Using PersistentOpenStruct: O(n) USING THE NOTATION YOU LOVE!

Slide 104

Slide 104 text

THE INFORMATION YOU CRAVE • Let n = number of methods to define • Let o = objects to create • Using PersistentOpenStruct: O(n) • Using OpenStruct: O(no) USING THE NOTATION YOU LOVE!

Slide 105

Slide 105 text

MATH PROVIDES A USEFUL FRAMEWORK FOR THINKING ABOUT PROBLEMS. AS ENGINEERS, WE NEED ANSWERS GROUNDED IN REALITY.

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

BENCHMARK YOUR LIBRARY

Slide 108

Slide 108 text

BENCHMARK YOUR LIBRARY BENCHMARK YOUR APP

Slide 109

Slide 109 text

BENCHMARK YOUR LIBRARY BENCHMARK YOUR APP BENCHMARK ALL THE THINGS!

Slide 110

Slide 110 text

PERSISTENTOPENSTRUCT IS GOOD WHEN REPEATEDLY CREATING DATA OBJECTS WITH THE SAME SHAPE

Slide 111

Slide 111 text

ROUND 3: OpenStruct

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

INTRODUCED IN RUBY 2.3!

Slide 115

Slide 115 text

THIS IS A DRAMATIC IMPROVEMENT WHEN SOME KEYS ARE NEVER ACCESSED

Slide 116

Slide 116 text

ROUND 4: DynamicClass

Slide 117

Slide 117 text

SAMPLE USAGE DynamicClass

Slide 118

Slide 118 text

SAMPLE USAGE DynamicClass Animal = DynamicClass.new do def speak "The #{type} makes a #{sound} sound!" end end

Slide 119

Slide 119 text

SAMPLE USAGE DynamicClass Animal = DynamicClass.new do def speak "The #{type} makes a #{sound} sound!" end end dog = Animal.new(type: 'dog', sound: 'woof') # => # dog.speak # => "The dog makes a woof sound!" dog.ears = 'droopy' dog[:nose] = ['cold', 'wet'] dog['tail'] = 'waggable' dog # => #

Slide 120

Slide 120 text

SAMPLE USAGE DynamicClass Animal = DynamicClass.new do def speak "The #{type} makes a #{sound} sound!" end end dog = Animal.new(type: 'dog', sound: 'woof') # => # dog.speak # => "The dog makes a woof sound!" dog.ears = 'droopy' dog[:nose] = ['cold', 'wet'] dog['tail'] = 'waggable' dog # => # Animal.instance_methods(false) # => [:to_h, :speak, :type=, :type, :sound=, :sound, :ears=, :ears,
 :nose=, :nose, :tail=, :tail]

Slide 121

Slide 121 text

HOW IT WORKS DynamicClass class << self def attributes @attributes ||= Set.new end def add_methods!(key) class_exec do attr_writer key unless method_defined?("#{key}=") attr_reader key unless method_defined?("#{key}") attributes << key end end end

Slide 122

Slide 122 text

HOW IT WORKS DynamicClass

Slide 123

Slide 123 text

HOW IT WORKS DynamicClass def initialize(attributes = {}) attributes.each_pair do |key, value| __send__(:"#{key}=", value) end end

Slide 124

Slide 124 text

HOW IT WORKS DynamicClass def method_missing(mid, *args) if (mname = mid[/.*(?==\z)/m]) self[mname] = args.first else self[mid] end end def initialize(attributes = {}) attributes.each_pair do |key, value| __send__(:"#{key}=", value) end end

Slide 125

Slide 125 text

HOW IT WORKS DynamicClass def method_missing(mid, *args) if (mname = mid[/.*(?==\z)/m]) self[mname] = args.first else self[mid] end end def initialize(attributes = {}) attributes.each_pair do |key, value| __send__(:"#{key}=", value) end end animal.baz = 4

Slide 126

Slide 126 text

HOW IT WORKS DynamicClass def method_missing(mid, *args) if (mname = mid[/.*(?==\z)/m]) self[mname] = args.first else self[mid] end end def initialize(attributes = {}) attributes.each_pair do |key, value| __send__(:"#{key}=", value) end end animal.baz = 4 animal.baz

Slide 127

Slide 127 text

HOW IT WORKS DynamicClass def method_missing(mid, *args) if (mname = mid[/.*(?==\z)/m]) self[mname] = args.first else self[mid] end end def []=(key, value) key = key.to_sym instance_variable_set(:"@#{key}", value) unless self.class.attributes.include?(key) self.class.add_methods!(key) end end def [](key) instance_variable_get(:"@#{key}") end def initialize(attributes = {}) attributes.each_pair do |key, value| __send__(:"#{key}=", value) end end animal.baz = 4 animal.baz

Slide 128

Slide 128 text

HOW IT WORKS DynamicClass def method_missing(mid, *args) if (mname = mid[/.*(?==\z)/m]) self[mname] = args.first else self[mid] end end def []=(key, value) key = key.to_sym instance_variable_set(:"@#{key}", value) unless self.class.attributes.include?(key) self.class.add_methods!(key) end end def [](key) instance_variable_get(:"@#{key}") end def initialize(attributes = {}) attributes.each_pair do |key, value| __send__(:"#{key}=", value) end end animal.baz = 4 animal.baz Instead of an internal hash @table, we use instance variables just as you would in a standard class!

Slide 129

Slide 129 text

IS IT FAST? Comparison: RegularClass: 3757567.8 i/s DynamicClass: 1250634.2 i/s - 3.00x slower PersistentOpenStruct: 766792.7 i/s - 4.90x slower OpenFastStruct: 525565.1 i/s - 7.15x slower OpenStruct: 147361.4 i/s - 25.50x slower

Slide 130

Slide 130 text

DYNAMICCLASS IS GOOD FOR THE SAME PURPOSES AS PERSISTENTOPENSTRUCT BUT IT’S FASTER

Slide 131

Slide 131 text

DYNAMICCLASS WORKS INTERNALLY THE WAY THAT PERSISTENTOPENSTRUCT REALLY SHOULD

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

PERSISTENTOPENSTRUCT MADE ME HAPPY.

Slide 134

Slide 134 text

PERSISTENTOPENSTRUCT MADE ME HAPPY. DYNAMICCLASS
 MADE ME HAPPIER.

Slide 135

Slide 135 text

PERSISTENTOPENSTRUCT MADE ME HAPPY. DYNAMICCLASS
 MADE ME HAPPIER. THAT’S THE BOTTOM LINE.

Slide 136

Slide 136 text

ROUNDUP: WHAT HAVE WE LEARNED?

Slide 137

Slide 137 text

ROUNDUP: WHAT HAVE WE LEARNED?

Slide 138

Slide 138 text

ROUNDUP: WHAT HAVE WE LEARNED? • Even the Standard Library may have room for improvement!

Slide 139

Slide 139 text

ROUNDUP: WHAT HAVE WE LEARNED? • Even the Standard Library may have room for improvement! • By focusing on a particular use case, you might create something that works better than a standard tool.

Slide 140

Slide 140 text

ROUNDUP: WHAT HAVE WE LEARNED? • Even the Standard Library may have room for improvement! • By focusing on a particular use case, you might create something that works better than a standard tool. • Benchmark, benchmark, benchmark!

Slide 141

Slide 141 text

ROUNDUP: WHAT HAVE WE LEARNED? • Even the Standard Library may have room for improvement! • By focusing on a particular use case, you might create something that works better than a standard tool. • Benchmark, benchmark, benchmark! • Experiments are probably the most satisfying, internally rewarding activity in all of programming. Sometimes you gain something useful, other times it’s just fun.

Slide 142

Slide 142 text

ROUNDUP: WHAT HAVE WE LEARNED? • Even the Standard Library may have room for improvement! • By focusing on a particular use case, you might create something that works better than a standard tool. • Benchmark, benchmark, benchmark! • Experiments are probably the most satisfying, internally rewarding activity in all of programming. Sometimes you gain something useful, other times it’s just fun. • Whoever you are, you have something to offer!

Slide 143

Slide 143 text

THANK YOU! ARIEL CAPLAN @AMCAPLAN AMCAPLAN.NINJA