# Ruby Quiz #164 - Price Ranges
# The PriceRange class represents both the desired price range of the
# shopper and the price ranges of the companies.
## company_a = PriceRange.new(:low => 1_000, :high => 3_000)
# company_b = PriceRange.new(:low => 6_000, :high => 10_000)
# company_c = PriceRange.new(:low => 500, :high => 2_500)
## customer = PriceRange.new(:low => 2_500, :high => 5_000)
## The low and high points are both optional and default to zero and
# infinity respectively.
## Once you have the price ranges set up, it's as easy as using the
# select method to determine which company the shopper should examine.
## customer.select(company_a, company_b, company_c)
# # => [company_a, company_c]
## I decided against using Ruby's Range class for unremembered reasons.
## I didn't leave anything out did I?
classPriceRangeattr_reader:low,:highdefinitialize(opts={})@low= opts[:low]||0@high= opts[:high]||1.0/0.0enddefincludes_price?(price)
price >=@lowand price <=@highenddefincludes_edge_of?(other)
includes_price?(other.low)or includes_price?(other.high)enddefselect(*ranges)
ranges.select {|range|self.includes_edge_of?(range)or range.includes_edge_of?(self)}endend