Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # LSP-compliant solution: replace inheritance with delegation
- # Ruby's duck typing still lets you use a square in most places where
- # rectangle would be used - but no longer a subclass in LSP sense.
- class Square
- attr_accessor :rect
- def initialize(side, top_left)
- @rect = Rectangle.new(side, side, top_left)
- end
- def area ; rect.area ; end
- def perimeter ; rect.perimeter ; end
- # A more concise way to delegate, if using ActiveSupport (see text):
- # delegate :area, :perimeter, :to => :rectangle
- def side=(s) ; rect.width = rect.height = s ; end
- end
Add Comment
Please, Sign In to add comment