From: Steven Hahn Date: 2008-06-03T04:56:19+09:00 Subject: Re: [QUIZ] Price Ranges (#164) --_dd5236a6-866b-4229-b324-0911de128a33_ Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable I had not created a SAX listener before in Ruby. So, my solution reads the= vendor list in from XML. I noticed others were using this pastie thingy..= . I don't know what that is. Besides, pasties always seemed to be kind of = pointless to me. They leave precious little to the imagination. So, below= is the ruby file followed by the xml file: ---------------------------------------------------------------------------= ------ #!/usr/local/bin/ruby -w require 'rexml/parsers/sax2parser' require 'rexml/sax2listener' class VendorListener include REXML::SAX2Listener def initialize(low, high) begin @low =3D Float(low) rescue @low =3D 0 end =20 begin @high =3D Float(high) rescue #if someone can spend more than this then #they can afford a better program @high =3D 10**15 end end =20 def start_element(uri, localname, tag_name, attrs) if tag_name =3D=3D 'Vendor' @vendorName =3D attrs['name'] end end def end_element(uri, localname, tag_name) if tag_name =3D=3D 'Vendor' if in_range? puts @vendorName end elsif tag_name =3D=3D 'LowPrice' @lowPrice =3D Float(@data) elsif tag_name =3D=3D 'HighPrice' @highPrice =3D Float(@data) end end def characters(value) @data =3D value end def in_range? (@low >=3D @lowPrice and @low <=3D @highPrice) or (@high >=3D @lowPrice and @high <=3D @highPrice) or (@low < @lowPrice and @high > @highPrice) end end puts "Enter a price range." print "Enter low value: " low =3D gets print "Enter high value: " high =3D gets parser =3D REXML::Parsers::SAX2Parser.new(File.new('vendors.xml' )) parser.listen(VendorListener.new(low, high)) parser.parse puts "Program terminated." ---------------------------------------------------------------------------= ------ 1000 3000 6000 10000 500 2500 _________________________________________________________________ Make every e-mail and IM count. Join the i=92m Initiative from Microsoft. http://im.live.com/Messenger/IM/Join/Default.aspx?source=3DEML_WL_ MakeCoun= t= --_dd5236a6-866b-4229-b324-0911de128a33_--