Skip to content

Instantly share code, notes, and snippets.

@skorfmann
Created March 31, 2010 10:57
Show Gist options
  • Save skorfmann/350196 to your computer and use it in GitHub Desktop.
Save skorfmann/350196 to your computer and use it in GitHub Desktop.
class ShippingOption # Base class
@children = []
def self.inherited(child)
@children << child
end
def self.shipping_options(weight, international)
@children.select {|child| child.can_ship?(weight, international)}
end
end
class MediaMail < ShippingOption
def self.can_ship?(weight, international)
!international
end
end
class FlatRatePriorityEnvelope < ShippingOption
def self.can_ship?(weight, international)
weight < 64 && !international
end
end
class InternationalFlatRateBox < ShippingOption
def self.can_ship?(weight, international)
weight >= 64 && weight < 9*16 && international
end
end
puts "Shipping 16oz domestic"
puts ShippingOption.shipping_options(16, false)
puts "\nShipping 90oz domestic"
puts ShippingOption.shipping_options(90, false)
puts "\nShipping 16oz international"
puts ShippingOption.shipping_options(16, true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment