Created
February 21, 2012 01:48
-
-
Save maxim/1872879 to your computer and use it in GitHub Desktop.
Passing classes vs objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Passing classes | |
| class Item | |
| def apply_coupon(coupon_class) | |
| coupon_class.new(self).apply | |
| end | |
| end | |
| class Coupon | |
| attr_reader :target | |
| def initialize(target) | |
| @target = target | |
| end | |
| end | |
| class SomeCoupon < Coupon | |
| def apply | |
| target.price -= 10_00 | |
| end | |
| end | |
| # Passing objects | |
| class Item | |
| def apply_coupon(coupon) | |
| coupon.apply_to(self) | |
| end | |
| end | |
| class Coupon | |
| attr_reader :target | |
| def apply_to(target) | |
| @target = target | |
| apply | |
| end | |
| end | |
| class SomeCoupon < Coupon | |
| def apply | |
| target.price -= 10_00 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment