Slide 1

Slide 1 text

Thinking in Objects

Slide 2

Slide 2 text

@joshsusser CTO - Cognitive Health Innovations RubyRogues GoGaRuCo

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Perspective is worth 80 I.Q. points. –Alan Kay

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

http://www.flickr.com/photos/xlibber/2962488972

Slide 7

Slide 7 text

Object = Identity + Behavior + State OOP = Objects + Encapsulation + Polymorphism + Inheritance

Slide 8

Slide 8 text

It’s all about MESSAGES

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Smalltalk (OOP) LISP (FP) Perl (scripting)

Slide 11

Slide 11 text

Object = Identity + Behavior + State

Slide 12

Slide 12 text

Identity $ nslookup github.com 207.97.227.239

Slide 13

Slide 13 text

Identity s = "foo" s.object_id #=> 123450 t = "foo" #=> "foo" t.object_id #=> 123780

Slide 14

Slide 14 text

Identity s = "foo"; t = "foo" s == t #=> true s.equal?(t) #=> false

Slide 15

Slide 15 text

Identity s = "foo"; t = "foo" s << "d" #=> "food" t #=> "foo"

Slide 16

Slide 16 text

Behavior "foo".methods #=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :reverse, ...]

Slide 17

Slide 17 text

methods def even? self % 2 == 0 end def odd? !self.even? end

Slide 18

Slide 18 text

Object = Identity + Behavior + State

Slide 19

Slide 19 text

uniques nil true, false :ruby, :rails

Slide 20

Slide 20 text

uniques GUEST = Object.new def GUEST.name "Guest User" end

Slide 21

Slide 21 text

State s = "foo" s << "d" #=> "food" s.length #=> 4

Slide 22

Slide 22 text

State = Behavior

Slide 23

Slide 23 text

Uniform Access attr_accessor :first_name

Slide 24

Slide 24 text

Uniform Access def first_name @first_name end

Slide 25

Slide 25 text

Uniform Access def first_name @full_name.split(" ").first end

Slide 26

Slide 26 text

JavaScript user.firstName user.firstName()

Slide 27

Slide 27 text

GET /index •WEB_ROOT/index.html •app/views/home/index.erb •memcached / varnish

Slide 28

Slide 28 text

Encapsulation

Slide 29

Slide 29 text

Polymorphism Duck-typing Protocol API

Slide 30

Slide 30 text

Object-based Objects + Encapsulation + Polymorphism dynamic binding

Slide 31

Slide 31 text

Inheritance • Implementation • Type

Slide 32

Slide 32 text

Delegation • Composition - Forwardable • Prototypes - JavaScript, SELF

Slide 33

Slide 33 text

Delegation IS Inheritance Lynn Andrea Stein OOPSLA 1987 "Treaty of Orlando"

Slide 34

Slide 34 text

OOP= Objects + Encapsulation + Polymorphism + Inheritance

Slide 35

Slide 35 text

It’s all about MESSAGES

Slide 36

Slide 36 text

call a method

Slide 37

Slide 37 text

• delegation • method_missing call a method

Slide 38

Slide 38 text

send a message call a method

Slide 39

Slide 39 text

class Person def name; @name; end end class Employee < Person def name; "#{@name}, #{@title}"; end end class Letter # address letter to recipient's name without title def recipient_name super_name = Person.instance_method(:name) bound_name = super_name.bind(@recipient) bound_name.call end end

Slide 40

Slide 40 text

class Person def name; @name; end end class Employee < Person def name; "#{@name}, #{@title}"; end end class Letter # address letter to recipient's name without title def recipient_name super_name = Person.instance_method(:name) bound_name = super_name.bind(@recipient) bound_name.call end end

Slide 41

Slide 41 text

use objects •blocks & lambdas •hashes & arrays •symbols (names) instead of

Slide 42

Slide 42 text

use objects reification unites behavior with data

Slide 43

Slide 43 text

validation validates_presence_of :phone validate { |r| r.home_phone? || r.cell_phone? } validate :validate_has_a_phone validates_with HasAPhone

Slide 44

Slide 44 text

polymorphism Method lookup is fast. Testing class type is slow and messy.

Slide 45

Slide 45 text

def scope if @product if current_api_user.has_spree_role?("admin") unless params[:show_deleted] variants = @product.variants_including_master else variants = @product.variants_including_master_and_deleted end else variants = @product.variants_including_master end else variants = Variant.scoped if current_api_user.has_spree_role?("admin") unless params[:show_deleted] variants = Variant.active end end end variants end

Slide 46

Slide 46 text

def scope if @product if current_api_user.has_spree_role?("admin") unless params[:show_deleted] variants = @product.variants_including_master else variants = @product.variants_including_master_and_deleted end else variants = @product.variants_including_master end else variants = Variant.scoped if current_api_user.has_spree_role?("admin") unless params[:show_deleted] variants = Variant.active end end end variants end

Slide 47

Slide 47 text

class Variant class << self alias_method :variants_including_master_and_deleted, :scoped alias_method :variants_including_master, :active end end def scope scoper = @product || Variant if current_api_user.has_spree_role?("admin") && params[:show_deleted] scoper.variants_including_master_and_deleted else scoper.variants_including_master end end

Slide 48

Slide 48 text

Object = Identity + Behavior + State OOP = Objects + Encapsulation + Polymorphism + Inheritance Messages = secret sauce