Slide 1

Slide 1 text

Metaprogramming with `super` powers Bradley Schaefer @soulcutter

Slide 2

Slide 2 text

No content

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

What is metaprogramming? “Drawing Hands” by M.C. Escher. [reference]

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

UTC = ActiveSupport::TimeZone["UTC"] class Datapoint < ApplicationRecord def datapoint_at(tz = UTC) tz.at(datapoint_ts) end end class ChartAnnotation < ApplicationRecord def annotation_at(tz = UTC) tz.at(annotation_ts) end end

Slide 11

Slide 11 text

UTC = ActiveSupport::TimeZone["UTC"] class Datapoint < ApplicationRecord def datapoint_at(tz = UTC) tz.at(datapoint_ts) end end class ChartAnnotation < ApplicationRecord def annotation_at(tz = UTC) tz.at(annotation_ts) end end

Slide 12

Slide 12 text

UTC = ActiveSupport::TimeZone["UTC"] class Datapoint < ApplicationRecord def datapoint_at(tz = UTC) tz.at(datapoint_ts) end end class ChartAnnotation < ApplicationRecord def annotation_at(tz = UTC) tz.at(annotation_ts) end end

Slide 13

Slide 13 text

UTC = ActiveSupport::TimeZone["UTC"] class Datapoint < ApplicationRecord def datapoint_at(tz = UTC) tz.at(datapoint_ts) end end class ChartAnnotation < ApplicationRecord def annotation_at(tz = UTC) tz.at(annotation_ts) end end

Slide 14

Slide 14 text

First Attempt

Slide 15

Slide 15 text

module Timify def timify(attr) at_attr = attr.to_s.chomp("ts") + "at" define_method(at_attr) do |tz = UTC| tz.at(public_send(attr)) end end end class Datapoint extend Timify timify :datapoint_ts def datapoint_at(tz = UTC) tz.at(datapoint_ts) end end

Slide 16

Slide 16 text

module Timify def timify(attr) at_attr = attr.to_s.chomp("ts") + "at" define_method(at_attr) do |tz = UTC| tz.at(public_send(attr)) end end end class Datapoint extend Timify timify :datapoint_ts def datapoint_at(tz = UTC) tz.at(datapoint_ts) end end

Slide 17

Slide 17 text

module Timify def timify(attr) at_attr = attr.to_s.chomp("ts") + "at" define_method(at_attr) do |tz = UTC| tz.at(public_send(attr)) end end end class Datapoint extend Timify timify :datapoint_ts end

Slide 18

Slide 18 text

This is why people hate metaprogramming

Slide 19

Slide 19 text

class Datapoint extend Timify timify :datapoint_ts end •"timify" is a verb, it's doing something to "datapoint_ts" - but what? •"datapoint_at" method comes out of nowhere •subtle problem with overriding

Slide 20

Slide 20 text

class Datapoint extend Timify timify :datapoint_ts end •"timify" is a verb, it's doing something to "datapoint_ts" - but what? •"datapoint_at" method comes out of nowhere •subtle problem with overriding

Slide 21

Slide 21 text

class Datapoint extend Timify timify :datapoint_ts end •"timify" is a verb, it's doing something to "datapoint_ts" - but what? •"datapoint_at" method comes out of nowhere •subtle problem with overriding

Slide 22

Slide 22 text

Second Attempt

Slide 23

Slide 23 text

Principle of Least Astonishment “People are part of the system. The design should match the user's experience, expectations, and mental models” –Principles of computer system design: an introduction ISBN 978-0-12-374957-4

Slide 24

Slide 24 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end class Datapoint extend TimeAttributeMethods time_reader :datapoint_at, from: :datapoint_ts end

Slide 25

Slide 25 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end class Datapoint extend TimeAttributeMethods time_reader :datapoint_at, from: :datapoint_ts end

Slide 26

Slide 26 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end class Datapoint extend TimeAttributeMethods time_reader :datapoint_at, from: :datapoint_ts end

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

class AlarmEvent extend TimeAttributeMethods time_reader :end_at, from: :end_ts def end_at(tz = UTC) return tz.now if end_ts.nil? super end end

Slide 29

Slide 29 text

class AlarmEvent extend TimeAttributeMethods time_reader :end_at, from: :end_ts def end_at(tz = UTC) return tz.now if end_ts.nil? super end end alarm_event.end_at # What happens?

Slide 30

Slide 30 text

class AlarmEvent extend TimeAttributeMethods time_reader :end_at, from: :end_ts def end_at(tz = UTC) return tz.now if end_ts.nil? super end end alarm_event.end_at # What happens? NoMethodError: super: no superclass method `end_at' for #

Slide 31

Slide 31 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end time_reader :end_at, from: :end_ts def end_at(tz = UTC) tz.at(end_ts) end def end_at(tz = UTC) return tz.now if end_ts.nil? super end

Slide 32

Slide 32 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end time_reader :end_at, from: :end_ts def end_at(tz = UTC) tz.at(end_ts) end def end_at(tz = UTC) return tz.now if end_ts.nil? super end

Slide 33

Slide 33 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end time_reader :end_at, from: :end_ts def end_at(tz = UTC) tz.at(end_ts) end def end_at(tz = UTC) return tz.now if end_ts.nil? super end

Slide 34

Slide 34 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end time_reader :end_at, from: :end_ts def end_at(tz = UTC) tz.at(end_ts) end def end_at(tz = UTC) return tz.now if end_ts.nil? super end

Slide 35

Slide 35 text

module TimeAttributeMethods def time_reader(attr, from:) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end time_reader :end_at, from: :end_ts def end_at(tz = UTC) tz.at(end_ts) end def end_at(tz = UTC) return tz.now if end_ts.nil? super end

Slide 36

Slide 36 text

Third Attempt

Slide 37

Slide 37 text

module TimeAttributeMethods def time_reader(attr, from:) include (Module.new do |mod| define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end) end end time_reader :datapoint_at, from: :datapoint_ts Datapoint.ancestors # => [Datapoint, #, Object, PP::ObjectMixin, Kernel, BasicObject]

Slide 38

Slide 38 text

time_reader :end_at, from: :end_ts def end_at(tz = UTC) return tz.now if end_ts.nil? super end AlarmEvent.ancestors # => [ AlarmEvent, #, Object, Kernel, BasicObject ]

Slide 39

Slide 39 text

time_reader :end_at, from: :end_ts def end_at(tz = UTC) return tz.now if end_ts.nil? super end AlarmEvent.ancestors # => [ AlarmEvent, #, Object, Kernel, BasicObject ] super

Slide 40

Slide 40 text

time_reader :end_at, from: :end_ts def end_at(tz = UTC) return tz.now if end_ts.nil? super end AlarmEvent.ancestors # => [ AlarmEvent, #, Object, Kernel, BasicObject ] super

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

pry(main)> ls Datapoint.new ##methods: datapoint_at Datapoint#methods: datapoint_ts pry(main)> $ Datapoint.new.datapoint_at From: /Users/soulcutter/super-powers/ time_attribute_methods.rb @ line 4: Owner: # Visibility: public Number of lines: 3 define_method(attr) do |tz = UTC| tz.at(public_send(from)) end

Slide 44

Slide 44 text

pry(main)> ls Datapoint.new ##methods: datapoint_at pry(main)> $ Datapoint.new.datapoint_at From: /Users/soulcutter/super-powers/ time_attribute_methods.rb @ line 4: Owner: # Visibility: public Number of lines: 3 define_method(attr) do |tz = UTC| tz.at(public_send(from)) end

Slide 45

Slide 45 text

Final Attempt

Slide 46

Slide 46 text

The Module Builder Pattern

Slide 47

Slide 47 text

1.Define a subclass of Module 2.Define an initializer that takes configuration 3.Use configuration to define methods

Slide 48

Slide 48 text

class TimeAttribute < Module def initialize(attr, from) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end module TimeAttributeMethods def time_reader(attr, from:) include TimeAttribute.new(attr, from) end end

Slide 49

Slide 49 text

class TimeAttribute < Module def initialize(attr, from) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end module TimeAttributeMethods def time_reader(attr, from:) include TimeAttribute.new(attr, from) end end

Slide 50

Slide 50 text

class TimeAttribute < Module def initialize(attr, from) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end module TimeAttributeMethods def time_reader(attr, from:) include TimeAttribute.new(attr, from) end end

Slide 51

Slide 51 text

class TimeAttribute < Module def initialize(attr, from) define_method(attr) do |tz = UTC| tz.at(public_send(from)) end end end module TimeAttributeMethods def time_reader(attr, from:) include TimeAttribute.new(attr, from) end end

Slide 52

Slide 52 text

pry(main)> ls Datapoint.new ##methods: datapoint_at Datapoint#methods: datapoint_ts pry(main)> $ Datapoint.new.datapoint_at From: /Users/soulcutter/super-powers/ time_attribute_methods.rb @ line 4: Owner: # Visibility: public Number of lines: 3 define_method(attr) do |tz = UTC| tz.at(public_send(from))

Slide 53

Slide 53 text

def inspect "#<#{self.class.name}: attr=#{@attr.inspect} \ from=#{@from.inspect}>" end pry(main)> ls Datapoint.new ##methods: datapoint_at Datapoint#methods: datapoint_ts

Slide 54

Slide 54 text

def inspect "#<#{self.class.name}: attr=#{@attr.inspect} \ from=#{@from.inspect}>" end pry(main)> ls Datapoint.new ##methods: datapoint_at Datapoint#methods: datapoint_ts

Slide 55

Slide 55 text

Metaprogramming:

Slide 56

Slide 56 text

Metaprogramming with `super` powers Bradley Schaefer @soulcutter Source: Adventure Time Galactic Fist Bump T-Shirt