• Like
  • Save
OOP Intro in Ruby for NHRuby Feb 2010
Upcoming SlideShare
Loading in...5
×

OOP Intro in Ruby for NHRuby Feb 2010

  • 549 views
Uploaded on

Introduction to Object Oriented Design in Ruby for the February 2010 NHRuby (http://nhruby.org) meeting.

Introduction to Object Oriented Design in Ruby for the February 2010 NHRuby (http://nhruby.org) meeting.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • interesting presentation about OOP in Ruby
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
549
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
1
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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)
  • 8. Interface Implementation
  • 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. 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
  • 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. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 20. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 21. Bicycle Motorcycle
  • 22. 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
  • 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. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  • 26. Abstraction Encapsulation Inheritance (Hierarchy) Dynamic Binding (Polymorphism)
  • 27. 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
  • 28. 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
  • 29. Abstraction Encapsulation Inheritance (Hierarchy) Dynamic Binding (Polymorphism)
  • 30. Class Methods Composition Duck Typing Mixins
  • 31. Class Methods Composition Duck Typing Mixins
  • 32. 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! $
  • 33. Class Methods Composition Duck Typing Mixins
  • 34. Class Methods Composition Duck Typing Mixins
  • 35. 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
  • 36. Class Methods Composition Duck Typing Mixins
  • 37. Class Methods Composition Duck Typing Mixins
  • 38. def read_file(stream) stream.read if stream.is_a?(File) end def read_file(stream) stream.read if stream.respond_to(:read) end
  • 39. Class Methods Composition Duck Typing Mixins
  • 40. Class Methods Composition Duck Typing Mixins
  • 41. Class Methods Composition Duck Typing Mixins
  • 42. The Complete Class
  • 43. Programming Exercise
  • 44. 1 2 3 4 5 6 7 8
  • 45. Solve It! 3 8 6 7 1 5 2 4 git://github.com/bturnbull/tile_puzzle.git
  • 46. class Puzzle def initialize(size = 3) end def move(dx, dy) end def solved? end end
  • 47. 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/