Advertisement
saasbook

lsp_square_rect.rb

Aug 15th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.64 KB | None | 0 0
  1. class Rectangle
  2.   attr_accessor :width, :height, :top_left_corner
  3.   def new(width,height,top_left) ... ; end
  4.   def area ... ; end
  5.   def perimeter ... ; end
  6. end
  7. # A square is just a special case of rectangle...right?
  8. class Square < Rectangle
  9.   # ooops...a square has to have width and height equal
  10.   attr_reader :width, :height, :side
  11.   def width=(w)  ; @width = @height = w ; end
  12.   def height=(w) ; @width = @height = w ; end
  13.   def side=(w)   ; @width = @height = w ; end
  14. end
  15. # But is a Square really a kind of Rectangle?
  16. class Rectangle
  17.   def make_twice_as_wide_as_high(dim)
  18.     self.width = 2*dim
  19.     self.height = dim           # doesn't work!
  20.   end
  21. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement