OOP Intro in Ruby for NHRuby Feb 2010

983 views

Published on

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

1 Comment
5 Likes
Statistics
Notes
  • interesting presentation about OOP in Ruby
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
983
On SlideShare
0
From Embeds
0
Number of Embeds
29
Actions
Shares
0
Downloads
0
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide

OOP Intro in Ruby for NHRuby Feb 2010

  1. 1. Object Oriented Design in Ruby Brian Turnbull NHRuby
  2. 2. Phone Concrete Clock Lamp Stack Abstract Mutex Message
  3. 3. Class Instantiation Object
  4. 4. Class.new Initialize Object Instance
  5. 5. class CloneTrooper def march ... end end trooper = CloneTrooper.new trooper.march
  6. 6. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  7. 7. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  8. 8. Interface Implementation
  9. 9. Stack E E Push Pop E D D D C C C B B B A A A
  10. 10. Leaky Stack F E Push Push E F D D E C C D B B C A A B Discard A
  11. 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. 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. 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. 14. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  15. 15. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  16. 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. 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. 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. 19. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  20. 20. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  21. 21. Bicycle Motorcycle
  22. 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. 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. 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. 25. Abstraction Encapsulation Inheritance (Hierarchy) Polymorphism (Dynamic Binding)
  26. 26. Abstraction Encapsulation Inheritance (Hierarchy) Dynamic Binding (Polymorphism)
  27. 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. 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. 29. Abstraction Encapsulation Inheritance (Hierarchy) Dynamic Binding (Polymorphism)
  30. 30. Class Methods Composition Duck Typing Mixins
  31. 31. Class Methods Composition Duck Typing Mixins
  32. 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. 33. Class Methods Composition Duck Typing Mixins
  34. 34. Class Methods Composition Duck Typing Mixins
  35. 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. 36. Class Methods Composition Duck Typing Mixins
  37. 37. Class Methods Composition Duck Typing Mixins
  38. 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. 39. Class Methods Composition Duck Typing Mixins
  40. 40. Class Methods Composition Duck Typing Mixins
  41. 41. Class Methods Composition Duck Typing Mixins
  42. 42. The Complete Class
  43. 43. Programming Exercise
  44. 44. 1 2 3 4 5 6 7 8
  45. 45. Solve It! 3 8 6 7 1 5 2 4 git://github.com/bturnbull/tile_puzzle.git
  46. 46. class Puzzle def initialize(size = 3) end def move(dx, dy) end def solved? end end
  47. 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/

×