Skip to content

Instantly share code, notes, and snippets.

@maxim
Created February 21, 2012 01:48
Show Gist options
  • Select an option

  • Save maxim/1872879 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/1872879 to your computer and use it in GitHub Desktop.
Passing classes vs objects
# 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