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)
OOP Intro in Ruby for NHRuby Feb 2010
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
OOP Intro in Ruby for NHRuby Feb 2010
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
OOP Intro in Ruby for NHRuby Feb 2010
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

PDF
Fun never stops. introduction to haskell programming language
PDF
Functional Programming & Event Sourcing - a pair made in heaven
PDF
From Python to Scala
ODP
Object-Oriented Programming
PDF
Scala101, first steps with Scala
PDF
The Great Scala Makeover
PDF
TreSQL
PDF
F bound-types
Fun never stops. introduction to haskell programming language
Functional Programming & Event Sourcing - a pair made in heaven
From Python to Scala
Object-Oriented Programming
Scala101, first steps with Scala
The Great Scala Makeover
TreSQL
F bound-types

What's hot (14)

PDF
Scala-对Java的修正和超越
PPTX
PDF
Functional programming in java
PDF
Go Java, Go!
PPTX
Go Java, Go!
PDF
ruby1_6up
PDF
Beyond javascript using the features of tomorrow
PPT
Functional Programming In Java
PPTX
Scala - where objects and functions meet
ODP
Basic inheritance in JavaScript
PDF
Linear regression with R 2
PDF
Google Guava & EMF @ GTUG Nantes
PPTX
python beginner talk slide
KEY
About java
Scala-对Java的修正和超越
Functional programming in java
Go Java, Go!
Go Java, Go!
ruby1_6up
Beyond javascript using the features of tomorrow
Functional Programming In Java
Scala - where objects and functions meet
Basic inheritance in JavaScript
Linear regression with R 2
Google Guava & EMF @ GTUG Nantes
python beginner talk slide
About java
Ad

Viewers also liked (20)

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

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

PDF
Clojure for Java developers - Stockholm
PDF
アプリを弄ってみる #1 #antama_ws
PDF
So various polymorphism in Scala
PDF
Threequals - Case Equality in Ruby
ODP
Generic Programming
PDF
Postobjektové programovanie v Ruby
PPTX
Intro to scala
ODP
AST Transformations
PPT
Scala introduction
KEY
Go &lt;-> Ruby
PDF
Scalding - Hadoop Word Count in LESS than 70 lines of code
ODP
Groovy Ast Transformations (greach)
KEY
Language supports it
PDF
Ruby 程式語言入門導覽
PDF
A Sceptical Guide to Functional Programming
ODP
Ast transformations
PPT
Scala presentation by Aleksandar Prokopec
PDF
An Introduction to Scala (2014)
KEY
(map Clojure everyday-tasks)
PDF
Predictably
Clojure for Java developers - Stockholm
アプリを弄ってみる #1 #antama_ws
So various polymorphism in Scala
Threequals - Case Equality in Ruby
Generic Programming
Postobjektové programovanie v Ruby
Intro to scala
AST Transformations
Scala introduction
Go &lt;-> Ruby
Scalding - Hadoop Word Count in LESS than 70 lines of code
Groovy Ast Transformations (greach)
Language supports it
Ruby 程式語言入門導覽
A Sceptical Guide to Functional Programming
Ast transformations
Scala presentation by Aleksandar Prokopec
An Introduction to Scala (2014)
(map Clojure everyday-tasks)
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)
  • 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
  • 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
  • 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/