From: Steven Hahn Date: 2008-06-04T03:46:54+09:00 Subject: Re: [QUIZ] Price Ranges (#164) --_6c74a6e5-c4e1-4b36-8cb1-bdca334c8c0a_ Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable I hadn't planned on making another submission, but after tinkering with it = a bit I realized a few things: 1. A simple GUI would be nice. I used FXRuby. 2. The SAX Listener was barfing when the number of vendors was six or more.= Funny thing was that it only had a problem when the XML was formatted. I= f I removed the whitespace it did just fine. In any case, I switched to us= ing a StreamListener instead. 3. There was an inconsistency in the logic in my original in_range? method. Anyway, here is the Ruby code followed by sample XML: -------------------------------------------------------------- #!/usr/local/bin/ruby -w require 'rexml/document' require 'rexml/streamlistener' require 'fox16' include Fox class VendorListener=20 include REXML::StreamListener def initialize(low_price, high_price, vendor_search_window) begin @low_price =3D Float(low_price) rescue @low_price =3D 0 end =20 begin @high_price =3D Float(high_price) rescue #if someone can spend more than this then #she or he can afford a better program @high_price =3D 10**15 end =20 @vdr_srch_wdw =3D vendor_search_window end =20 def tag_start(name, attrs) if name =3D=3D 'Vendor' @vendor_name =3D attrs['name'] end end =20 def tag_end(name) if name =3D=3D 'Vendor' and=20 if in_range? @vdr_srch_wdw.add_vendor(@vendor_name) end elsif name =3D=3D 'LowPrice' @vendor_low_price =3D Float(@data) elsif name =3D=3D 'HighPrice' @vendor_high_price =3D Float(@data) end end def text(text) @data =3D text end def in_range? @low_price <=3D @high_price and (@low_price >=3D @vendor_low_price or @high_price >=3D @vendor_low_pr= ice) and (@low_price <=3D @vendor_high_price or @high_price <=3D @vendor_high_= price) end end class VendorSearchWindow < FXMainWindow def initialize(app) # Invoke base class initialize first super(app, "Ruby Quiz \#164: Vendor Search", nil, nil, DECOR_TITLE | DE= COR_CLOSE) #Add text field frame at the top textfields =3D FXHorizontalFrame.new(self, LAYOUT_SIDE_TOP|LAYOUT_CENTE= R_X) FXLabel.new(textfields, "Enter a range:", nil, JUSTIFY_LEFT) FXLabel.new(textfields, "low:", nil, JUSTIFY_RIGHT) @low_field =3D FXTextField.new(textfields, 10, nil, 0, JUSTIFY_RIGHT|FR= AME_SUNKEN|FRAME_THICK|LAYOUT_SIDE_TOP) FXLabel.new(textfields, "high:", nil, JUSTIFY_RIGHT) @high_field =3D FXTextField.new(textfields, 10, nil, 0, JUSTIFY_RIGHT|F= RAME_SUNKEN|FRAME_THICK|LAYOUT_SIDE_TOP) =20 #add button frame at the bottom buttons =3D FXHorizontalFrame.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_CENTE= R_X|PACK_UNIFORM_WIDTH) show_button =3D FXButton.new(buttons, "Show Vendors") show_button.connect(SEL_COMMAND, method(:on_show_vendors)) exit_button =3D FXButton.new(buttons, "Exit") exit_button.connect(SEL_COMMAND, method(:on_exit)) #Place the list in a sunken frame sunken_frame =3D FXHorizontalFrame.new(self, LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, :padding = =3D> 0) @vendor_list =3D FXList.new(sunken_frame, :opts =3D> LIST_SINGLESELECT|= LAYOUT_FILL_X|LAYOUT_FILL_Y) end def on_exit(sender, sel, ptr) getApp().exit end =20 def on_show_vendors(sender, sel, ptr) @vendor_list.clearItems(false) REXML::Document.parse_stream(File.new('vendors.xml' ), VendorListener.new(@low_field.text, @high_field.text, self)) end =20 def add_vendor(vndr_name) @vendor_list.appendItem(vndr_name) end =20 def create super show(PLACEMENT_SCREEN) end end application =3D FXApp.new VendorSearchWindow.new(application) application.create application.run -------------------------------------------------------------- 1000 3000 6000 10000 500 2500 _________________________________________________________________ Now you can invite friends from Facebook and other groups to join you on Wi= ndows Live=99 Messenger. Add now. https://www.invite2messenger.net/im/?source=3DTXT_EML_WLH_AddNow_Now= --_6c74a6e5-c4e1-4b36-8cb1-bdca334c8c0a_--