SlideShare a Scribd company logo
Object Oriented
Design in Ruby
     Brian Turnbull
       NHRuby
Phone
Concrete   Clock
           Lamp


           Stack
Abstract   Mutex
           Message
Class
   Instantiation




Object
Class.new
       Initialize




Object Instance
class CloneTrooper
  def march
    ...
  end
end




trooper = CloneTrooper.new
trooper.march
Abstraction
Encapsulation
 Inheritance
    (Hierarchy)

Polymorphism
  (Dynamic Binding)
Abstraction
Encapsulation
 Inheritance
    (Hierarchy)

Polymorphism
  (Dynamic Binding)
Interface




Implementation
Stack
E                        E
    Push           Pop
             E
      D      D     D
      C      C     C
      B      B     B
      A      A     A
Leaky Stack
           F
E              Push
    Push
                E     F
      D         D     E
      C         C     D
      B         B     C
      A         A     B
                          Discard


                            A
class LeakyStack
  def initialize(size)
    @size = size
    @stack = Array.new
  end

  def push(item)
    @stack.push(item)
    @stack.shift if @stack.size > @size
  end

  def pop
    @stack.pop
  end
end
class LeakyStack
  def initialize(size)
    @size = size
    @stack = Array.new
  end

  def push(item)
    @stack.push(item)
    @stack.shift if @stack.size > @size
  end

  def pop
    @stack.pop
  end
end
class LeakyStack
  def initialize(size)
    @size = size
    @stack = Array.new
  end

  def push(item)
    @stack.push(item)
    @stack.shift if @stack.size > @size
  end

  def pop
    @stack.pop
  end
end
Abstraction
Encapsulation
 Inheritance
    (Hierarchy)

Polymorphism
  (Dynamic Binding)
Abstraction
Encapsulation
 Inheritance
    (Hierarchy)

Polymorphism
  (Dynamic Binding)
class LeakyStack
  def initialize(size)
    @size = size
    @stack = Array.new
  end

  def push(item)
    @stack.push(item)
    discard_overflow
  end

  def pop
    @stack.pop
  end

private
  def discard_overflow
    @stack.shift if @stack.size > @size
  end
end
class LeakyStack
  def initialize(size)
    @size = size
    @stack = Array.new
  end

  def push(item)
    @stack.push(item)
    discard_overflow
  end

  def pop
    @stack.pop
  end

private
  def discard_overflow
    @stack.shift if @stack.size > @size
  end
end
class LeakyStack
  def initialize(size)
    @size = size
    @stack = Array.new
  end

  def push(item)
    @stack.push(item)
    discard_overflow
  end

  def pop
    @stack.pop
  end

private
  def discard_overflow
    @stack.shift if @stack.size > @size
  end
end
Abstraction
Encapsulation
 Inheritance
    (Hierarchy)

Polymorphism
  (Dynamic Binding)
Abstraction
Encapsulation
 Inheritance
    (Hierarchy)

Polymorphism
  (Dynamic Binding)
Bicycle


Motorcycle
class LeakyStack < Array
  def initialize(size)
    @stack_size = size
    super()
  end

  def push(item)
    super(item)
    discard_overflow
  end

private
  def discard_overflow
    shift if size > @stack_size
  end
end
class LeakyStack < Array
  def initialize(size)
    @stack_size = size
    super()
  end

  def push(item)
    super(item)
    discard_overflow
  end

private
  def discard_overflow
    shift if size > @stack_size
  end
end
class LeakyStack < Array
  def initialize(size)
    @stack_size = size
    super()
  end

  def push(item)
    super(item)
    discard_overflow
  end

private
  def discard_overflow
    shift if size > @stack_size
  end
end
Abstraction
Encapsulation
 Inheritance
    (Hierarchy)

Polymorphism
  (Dynamic Binding)
Abstraction
 Encapsulation
  Inheritance
     (Hierarchy)

Dynamic Binding
    (Polymorphism)
Message           stack = LeakyStack.new(10)
                  stack.push(“foo”)




Dynamic Binding
    Run Time

                  class LeakyStack < Array
                    def push(item)

Method                super(item)
                      discard_overflow
                    end
                  end
class LeakyStack < Array
  def initialize(size)
    @stack_size = size
    super()                       Array
  end

  def push(item)
    super(item)
    discard_overflow
  end
                           LeakyStack
private
  def discard_overflow
    shift if size > @stack_size
  end
end
Abstraction
 Encapsulation
  Inheritance
     (Hierarchy)

Dynamic Binding
    (Polymorphism)
Class Methods
 Composition
 Duck Typing
    Mixins
Class Methods
 Composition
 Duck Typing
    Mixins
class Foo
  def self.say_woot
    puts "woot!"
  end

  class << self
    def raise_roof
      2.times {say_woot}
    end
  end
end

$ Foo.raise_roof
woot!
woot!
$
Class Methods
 Composition
 Duck Typing
    Mixins
Class Methods
 Composition
 Duck Typing
    Mixins
class LeakyStack
  def initialize(size)
    @size = size
    @stack = Array.new
  end

  def push(item)
    @stack.push(item)
    @stack.shift if @stack.size > @size
  end

  def pop
    @stack.pop
  end
end
Class Methods
 Composition
 Duck Typing
    Mixins
Class Methods
 Composition
 Duck Typing
    Mixins
def read_file(stream)
  stream.read if stream.is_a?(File)
end

def read_file(stream)
  stream.read if stream.respond_to(:read)
end
Class Methods
 Composition
 Duck Typing
    Mixins
Class Methods
 Composition
 Duck Typing
    Mixins
Class Methods
 Composition
 Duck Typing
    Mixins
The Complete
    Class
Programming
  Exercise
1 2 3
4 5 6
7 8
Solve It!
              3 8
            6 7 1
            5 2 4
git://github.com/bturnbull/tile_puzzle.git
class Puzzle
  def initialize(size = 3)
  end

  def move(dx, dy)
  end

  def solved?
  end
end
Photo Credits in Order of Appearance
Untitled, Chaotic Good01,
http://www.flickr.com/photos/chaoticgood01/4037261441/

lego armee, loop_oh,
http://www.flickr.com/photos/loop_oh/3319379875/

Parliment Clock, Aldaron,
http://www.flickr.com/photos/aldaron/536362686/

Gears and Wheels, pietroizzo,
http://www.flickr.com/photos/pietroizzo/481609998/

Keeping Sound Transit under control, Oran Viriyincy,
http://www.flickr.com/photos/viriyincy/3412876205/

Chrissy's Toei Mixte, "A Touring Bicycle" Final Build, WillJL,
http://www.flickr.com/photos/willjl/3664435742/

Motorcycle 1, Custom Motorcycles,
http://www.flickr.com/photos/falconmotorcycles/2448989350/

~ Duck Talk ~, ViaMoi,
http://www.flickr.com/photos/viamoi/3605272991/

339/365 The ol’ pigskin, The Suss-Man (perpetually behind),
http://www.flickr.com/photos/8692813@N06/4162944190/

More Related Content

What's hot

Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越Caoyuan Deng
 
Gorm
GormGorm
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
John Ferguson Smart Limited
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
Andres Almiray
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
Andres Almiray
 
Beyond javascript using the features of tomorrow
Beyond javascript   using the features of tomorrowBeyond javascript   using the features of tomorrow
Beyond javascript using the features of tomorrow
Alexander Varwijk
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
Andrei Solntsev
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Basic inheritance in JavaScript
Basic inheritance in JavaScriptBasic inheritance in JavaScript
Basic inheritance in JavaScript
Brian Moschel
 
Linear regression with R 2
Linear regression with R 2Linear regression with R 2
Linear regression with R 2Kazuki Yoshida
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantesmikaelbarbero
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
jonycse
 
About java
About javaAbout java
About javaJay Xu
 

What's hot (14)

Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越
 
Gorm
GormGorm
Gorm
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
 
ruby1_6up
ruby1_6upruby1_6up
ruby1_6up
 
Beyond javascript using the features of tomorrow
Beyond javascript   using the features of tomorrowBeyond javascript   using the features of tomorrow
Beyond javascript using the features of tomorrow
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Basic inheritance in JavaScript
Basic inheritance in JavaScriptBasic inheritance in JavaScript
Basic inheritance in JavaScript
 
Linear regression with R 2
Linear regression with R 2Linear regression with R 2
Linear regression with R 2
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantes
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
About java
About javaAbout java
About java
 

Viewers also liked

Ruby OOP: Objects over Classes
Ruby OOP: Objects over ClassesRuby OOP: Objects over Classes
Ruby OOP: Objects over Classes
Aman King
 
Ruby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other MagicRuby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other Magic
Burke Libbey
 
Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.
Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.
Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.
IT-Доминанта
 
Elasticsearch Basics
Elasticsearch BasicsElasticsearch Basics
Elasticsearch Basics
Shifa Khan
 
Havas pr emilie parker art connection
Havas pr  emilie parker art connectionHavas pr  emilie parker art connection
Havas pr emilie parker art connectionHavas
 
Mind patterns and anti-patterns
Mind patterns and anti-patternsMind patterns and anti-patterns
Mind patterns and anti-patterns
Vladimir Dzhuvinov
 
Getting started book response
Getting started book   responseGetting started book   response
Getting started book responsepatpatpati
 
Capital autospa
Capital autospaCapital autospa
Capital autospa
Jake Shvilly
 
Yasmily laminario
Yasmily laminarioYasmily laminario
delitos informaticos
delitos informaticosdelitos informaticos
delitos informaticos
josling mireles
 
자료구조 그래프 보고서
자료구조 그래프 보고서자료구조 그래프 보고서
자료구조 그래프 보고서mil23
 
이벤트펜션 산정호수숙박
이벤트펜션 산정호수숙박이벤트펜션 산정호수숙박
이벤트펜션 산정호수숙박
jdhfrter
 
(Sadn1013 h) kump 15
(Sadn1013 h) kump 15(Sadn1013 h) kump 15
(Sadn1013 h) kump 15sadn1013
 
Happiest Minds is Hiring!!!!!!!
Happiest Minds is Hiring!!!!!!!Happiest Minds is Hiring!!!!!!!
Happiest Minds is Hiring!!!!!!!
Ashok K DL
 
Are you experienced (cont.)
Are you experienced (cont.) Are you experienced (cont.)
Are you experienced (cont.)
Royi benyossef
 
GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)
GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)
GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)
Jeremy W. Goldsmith
 
WAS-for-dummies
WAS-for-dummiesWAS-for-dummies

Viewers also liked (20)

Ruby OOP: Objects over Classes
Ruby OOP: Objects over ClassesRuby OOP: Objects over Classes
Ruby OOP: Objects over Classes
 
Ruby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other MagicRuby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other Magic
 
Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.
Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.
Основные понятия связанные с разработкой ПО: просто о сложном. Лаабе Дмитрий.
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Elasticsearch Basics
Elasticsearch BasicsElasticsearch Basics
Elasticsearch Basics
 
Havas pr emilie parker art connection
Havas pr  emilie parker art connectionHavas pr  emilie parker art connection
Havas pr emilie parker art connection
 
Mind patterns and anti-patterns
Mind patterns and anti-patternsMind patterns and anti-patterns
Mind patterns and anti-patterns
 
Getting started book response
Getting started book   responseGetting started book   response
Getting started book response
 
Capital autospa
Capital autospaCapital autospa
Capital autospa
 
Yasmily laminario
Yasmily laminarioYasmily laminario
Yasmily laminario
 
delitos informaticos
delitos informaticosdelitos informaticos
delitos informaticos
 
자료구조 그래프 보고서
자료구조 그래프 보고서자료구조 그래프 보고서
자료구조 그래프 보고서
 
이벤트펜션 산정호수숙박
이벤트펜션 산정호수숙박이벤트펜션 산정호수숙박
이벤트펜션 산정호수숙박
 
(Sadn1013 h) kump 15
(Sadn1013 h) kump 15(Sadn1013 h) kump 15
(Sadn1013 h) kump 15
 
Happiest Minds is Hiring!!!!!!!
Happiest Minds is Hiring!!!!!!!Happiest Minds is Hiring!!!!!!!
Happiest Minds is Hiring!!!!!!!
 
Are you experienced (cont.)
Are you experienced (cont.) Are you experienced (cont.)
Are you experienced (cont.)
 
GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)
GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)
GeoStories - Integration of Digital Media and Maps (GISCO Fall Meeting)
 
Planimetria camper
Planimetria camperPlanimetria camper
Planimetria camper
 
Education
EducationEducation
Education
 
WAS-for-dummies
WAS-for-dummiesWAS-for-dummies
WAS-for-dummies
 

Similar to OOP Intro in Ruby for NHRuby Feb 2010

Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 
アプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_wsアプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_ws
Takahiro Yoshimura
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scala
b0ris_1
 
Threequals - Case Equality in Ruby
Threequals - Case Equality in RubyThreequals - Case Equality in Ruby
Threequals - Case Equality in Ruby
Louis Scoras
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
PingLun Liao
 
Postobjektové programovanie v Ruby
Postobjektové programovanie v RubyPostobjektové programovanie v Ruby
Postobjektové programovanie v Ruby
Jano Suchal
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
AST Transformations
AST TransformationsAST Transformations
AST TransformationsHamletDRC
 
Scala introduction
Scala introductionScala introduction
Scala introduction
Yardena Meymann
 
Go &lt;-> Ruby
Go &lt;-> RubyGo &lt;-> Ruby
Go &lt;-> Ruby
Eleanor McHugh
 
Scalding - Hadoop Word Count in LESS than 70 lines of code
Scalding - Hadoop Word Count in LESS than 70 lines of codeScalding - Hadoop Word Count in LESS than 70 lines of code
Scalding - Hadoop Word Count in LESS than 70 lines of code
Konrad Malawski
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
HamletDRC
 
Language supports it
Language supports itLanguage supports it
Language supports it
Niranjan Paranjape
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Wen-Tien Chang
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingGarth Gilmour
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
HamletDRC
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
William Narmontas
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
Jacek Laskowski
 
Predictably
PredictablyPredictably
Predictablyztellman
 

Similar to OOP Intro in Ruby for NHRuby Feb 2010 (20)

Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
アプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_wsアプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_ws
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scala
 
Threequals - Case Equality in Ruby
Threequals - Case Equality in RubyThreequals - Case Equality in Ruby
Threequals - Case Equality in Ruby
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Postobjektové programovanie v Ruby
Postobjektové programovanie v RubyPostobjektové programovanie v Ruby
Postobjektové programovanie v Ruby
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Go &lt;-> Ruby
Go &lt;-> RubyGo &lt;-> Ruby
Go &lt;-> Ruby
 
Scalding - Hadoop Word Count in LESS than 70 lines of code
Scalding - Hadoop Word Count in LESS than 70 lines of codeScalding - Hadoop Word Count in LESS than 70 lines of code
Scalding - Hadoop Word Count in LESS than 70 lines of code
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 
Language supports it
Language supports itLanguage supports it
Language supports it
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
A Sceptical Guide to Functional Programming
A Sceptical Guide to Functional ProgrammingA Sceptical Guide to Functional Programming
A Sceptical Guide to Functional Programming
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
 
Predictably
PredictablyPredictably
Predictably
 

OOP Intro in Ruby for NHRuby Feb 2010

  • 1. Object Oriented Design in Ruby Brian Turnbull NHRuby
  • 2. Phone Concrete Clock Lamp Stack Abstract Mutex Message
  • 3. Class Instantiation Object
  • 4. Class.new Initialize Object Instance
  • 5. class CloneTrooper def march ... end end trooper = CloneTrooper.new trooper.march
  • 6. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 7. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 9. Stack E E Push Pop E D D D C C C B B B A A A
  • 10. Leaky Stack F E Push Push E F D D E C C D B B C A A B Discard A
  • 11. class LeakyStack def initialize(size) @size = size @stack = Array.new end def push(item) @stack.push(item) @stack.shift if @stack.size > @size end def pop @stack.pop end end
  • 12. class LeakyStack def initialize(size) @size = size @stack = Array.new end def push(item) @stack.push(item) @stack.shift if @stack.size > @size end def pop @stack.pop end end
  • 13. class LeakyStack def initialize(size) @size = size @stack = Array.new end def push(item) @stack.push(item) @stack.shift if @stack.size > @size end def pop @stack.pop end end
  • 14. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 15. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 16.
  • 17. class LeakyStack def initialize(size) @size = size @stack = Array.new end def push(item) @stack.push(item) discard_overflow end def pop @stack.pop end private def discard_overflow @stack.shift if @stack.size > @size end end
  • 18. class LeakyStack def initialize(size) @size = size @stack = Array.new end def push(item) @stack.push(item) discard_overflow end def pop @stack.pop end private def discard_overflow @stack.shift if @stack.size > @size end end
  • 19. class LeakyStack def initialize(size) @size = size @stack = Array.new end def push(item) @stack.push(item) discard_overflow end def pop @stack.pop end private def discard_overflow @stack.shift if @stack.size > @size end end
  • 20. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 21. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 23. class LeakyStack < Array def initialize(size) @stack_size = size super() end def push(item) super(item) discard_overflow end private def discard_overflow shift if size > @stack_size end end
  • 24. class LeakyStack < Array def initialize(size) @stack_size = size super() end def push(item) super(item) discard_overflow end private def discard_overflow shift if size > @stack_size end end
  • 25. class LeakyStack < Array def initialize(size) @stack_size = size super() end def push(item) super(item) discard_overflow end private def discard_overflow shift if size > @stack_size end end
  • 26. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 27. Abstraction Encapsulation Inheritance (Hierarchy) Dynamic Binding (Polymorphism)
  • 28. Message stack = LeakyStack.new(10) stack.push(“foo”) Dynamic Binding Run Time class LeakyStack < Array def push(item) Method super(item) discard_overflow end end
  • 29. class LeakyStack < Array def initialize(size) @stack_size = size super() Array end def push(item) super(item) discard_overflow end LeakyStack private def discard_overflow shift if size > @stack_size end end
  • 30. Abstraction Encapsulation Inheritance (Hierarchy) Dynamic Binding (Polymorphism)
  • 31. Class Methods Composition Duck Typing Mixins
  • 32. Class Methods Composition Duck Typing Mixins
  • 33. class Foo def self.say_woot puts "woot!" end class << self def raise_roof 2.times {say_woot} end end end $ Foo.raise_roof woot! woot! $
  • 34. Class Methods Composition Duck Typing Mixins
  • 35. Class Methods Composition Duck Typing Mixins
  • 36. class LeakyStack def initialize(size) @size = size @stack = Array.new end def push(item) @stack.push(item) @stack.shift if @stack.size > @size end def pop @stack.pop end end
  • 37. Class Methods Composition Duck Typing Mixins
  • 38. Class Methods Composition Duck Typing Mixins
  • 39.
  • 40. def read_file(stream) stream.read if stream.is_a?(File) end def read_file(stream) stream.read if stream.respond_to(:read) end
  • 41. Class Methods Composition Duck Typing Mixins
  • 42. Class Methods Composition Duck Typing Mixins
  • 43.
  • 44. Class Methods Composition Duck Typing Mixins
  • 45. The Complete Class
  • 47. 1 2 3 4 5 6 7 8
  • 48. Solve It! 3 8 6 7 1 5 2 4 git://github.com/bturnbull/tile_puzzle.git
  • 49. class Puzzle def initialize(size = 3) end def move(dx, dy) end def solved? end end
  • 50. Photo Credits in Order of Appearance Untitled, Chaotic Good01, http://www.flickr.com/photos/chaoticgood01/4037261441/ lego armee, loop_oh, http://www.flickr.com/photos/loop_oh/3319379875/ Parliment Clock, Aldaron, http://www.flickr.com/photos/aldaron/536362686/ Gears and Wheels, pietroizzo, http://www.flickr.com/photos/pietroizzo/481609998/ Keeping Sound Transit under control, Oran Viriyincy, http://www.flickr.com/photos/viriyincy/3412876205/ Chrissy's Toei Mixte, "A Touring Bicycle" Final Build, WillJL, http://www.flickr.com/photos/willjl/3664435742/ Motorcycle 1, Custom Motorcycles, http://www.flickr.com/photos/falconmotorcycles/2448989350/ ~ Duck Talk ~, ViaMoi, http://www.flickr.com/photos/viamoi/3605272991/ 339/365 The ol’ pigskin, The Suss-Man (perpetually behind), http://www.flickr.com/photos/8692813@N06/4162944190/