Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Thinking in Objects RubyConf 2012

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.

Josh Susser

November 03, 2012
Tweet

More Decks by Josh Susser

Other Decks in Programming

Transcript

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

    + Encapsulation + Polymorphism + Inheritance
  2. Identity s = "foo" s.object_id #=> 123450 t = "foo"

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

    #=> true s.equal?(t) #=> false
  4. Behavior "foo".methods #=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+,

    :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :reverse, ...]
  5. 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
  6. 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
  7. validation validates_presence_of :phone validate { |r| r.home_phone? || r.cell_phone? }

    validate :validate_has_a_phone validates_with HasAPhone
  8. 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
  9. 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
  10. 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
  11. Object = Identity + Behavior + State OOP = Objects

    + Encapsulation + Polymorphism + Inheritance Messages = secret sauce