Thinking in Objects RubyConf 2012

9f0f89bbd9e1ecfbaab6584e429b7a2f?s=47 Josh Susser
November 03, 2012

Thinking in Objects RubyConf 2012

This talk presents OOP fundamentals, and a basic vocabulary that helps to think about programming in OOP languages.

9f0f89bbd9e1ecfbaab6584e429b7a2f?s=128

Josh Susser

November 03, 2012
Tweet

Transcript

  1. Thinking in Objects

  2. @joshsusser CTO - Cognitive Health Innovations RubyRogues GoGaRuCo

  3. None
  4. Perspective is worth 80 I.Q. points. –Alan Kay

  5. None
  6. http://www.flickr.com/photos/xlibber/2962488972

  7. Object = Identity + Behavior + State OOP = Objects

    + Encapsulation + Polymorphism + Inheritance
  8. It’s all about MESSAGES

  9. None
  10. Smalltalk (OOP) LISP (FP) Perl (scripting)

  11. Object = Identity + Behavior + State

  12. Identity $ nslookup github.com 207.97.227.239

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

    #=> "foo" t.object_id #=> 123780
  14. Identity s = "foo"; t = "foo" s == t

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

    #=> "food" t #=> "foo"
  16. Behavior "foo".methods #=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+,

    :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :reverse, ...]
  17. methods def even? self % 2 == 0 end def

    odd? !self.even? end
  18. Object = Identity + Behavior + State

  19. uniques nil true, false :ruby, :rails

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

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

    #=> 4
  22. State = Behavior

  23. Uniform Access attr_accessor :first_name

  24. Uniform Access def first_name @first_name end

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

  26. JavaScript user.firstName user.firstName()

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

  28. Encapsulation

  29. Polymorphism Duck-typing Protocol API

  30. Object-based Objects + Encapsulation + Polymorphism dynamic binding

  31. Inheritance • Implementation • Type

  32. Delegation • Composition - Forwardable • Prototypes - JavaScript, SELF

  33. Delegation IS Inheritance Lynn Andrea Stein OOPSLA 1987 "Treaty of

    Orlando"
  34. OOP= Objects + Encapsulation + Polymorphism + Inheritance

  35. It’s all about MESSAGES

  36. call a method

  37. • delegation • method_missing call a method

  38. send a message call a method

  39. 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
  40. 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
  41. use objects •blocks & lambdas •hashes & arrays •symbols (names)

    instead of
  42. use objects reification unites behavior with data

  43. validation validates_presence_of :phone validate { |r| r.home_phone? || r.cell_phone? }

    validate :validate_has_a_phone validates_with HasAPhone
  44. polymorphism Method lookup is fast. Testing class type is slow

    and messy.
  45. 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
  46. 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
  47. 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
  48. Object = Identity + Behavior + State OOP = Objects

    + Encapsulation + Polymorphism + Inheritance Messages = secret sauce