Skip to content

Instantly share code, notes, and snippets.

@santosh79
Created November 17, 2009 20:12
Show Gist options
  • Save santosh79/237229 to your computer and use it in GitHub Desktop.
Save santosh79/237229 to your computer and use it in GitHub Desktop.
inherited callback
class ShippingOption
def self.children
@children ||= []
end
def self.inherited(child_class)
children << child_class
end
def self.for(weight, international)
children.select {|cls| cls.can_ship?(weight, international)}
end
end
class MediaMail < ShippingOption
def self.can_ship?(weight, international)
!international
end
end
class PriorityFlatRate < ShippingOption
def self.can_ship?(weight, international)
!international && weight < (4 * 16)
end
end
options = ShippingOption.for(3 * 16, false)
puts options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment