[wxruby-users] Problem with runtime error/segfault
Alex Fenton
alex at pressure.to
Tue Apr 15 09:46:30 EDT 2008
Hi Daniel
Thanks for the report. Unfortunately I couldn't test out your code
because it requires other files that you haven't included. We ask that
when posting a problem or bug, you simplify your code to demonstrate the
problem rather than just copying your enitre program.
http://wxruby.rubyforge.org/wiki/wiki.pl?MailingLists
I have a couple of suggestions on organisation and style that may help.
I think you could simplify your code quite a bit and that should help
iron out the bug.
My main one would be to use Wx::Timer rather than ruby Threads+sleep to
count down the time until the alarms go off. The way Ruby threads are
designed makes them ill-suited for many uses with GUI libraries, so
don't use them unless you really have to - and here, you don't.
> def editButtonClick(event)
> $whichAlarmVal = $whichAlarm.get_value()
> if $whichAlarmVal == 'Alarm # 1'
> require 'Sets1.rb'
> elsif $whichAlarmVal == 'Alarm # 2'
> require 'Sets2.rb'
>
Here you could make your code a lot simpler by using client_data. This
allows you to associate any normal ruby object (eg an Alarm object) with
an item in a ComboBox. So when the user clicks the edit button, you just
find the Alarm object associated with the currently selected value in
the ComboBox, and start editing it. You would then be able to handle an
arbitrary number of Alarms.
http://wxruby.rubyforge.org/doc/controlwithitems.html
> @dayText = StaticText.new(@aPanel, -1, "Days")
> @checkSun = CheckBox.new(@aPanel, -1, "Sunday")
> @checkMon = CheckBox.new(@aPanel, -1, "Monday")
> @checkTue = CheckBox.new(@aPanel, -1, "Tuesday")
A CheckListBox might be better here:
http://wxruby.rubyforge.org/doc/checklistbox.html
> def sets1
> File.open('Sets1.rb', 'w') do |f|
> f.puts "$statusDef = \"#{$comboStatusVal}\"
> $hourDef = \"#{$comboHourVal}\"
> $minDef = \"#{$comboMinVal}\"
There are much easier and safer ways to save an object with settings to
file. Look up "Marshal" or "YAML" for ruby - these will allow you to
save and load Alarms with no extra work.
> class Alarm
> def initialize
>
I would design your Alarm class to have attributes for the hour, minute
and second, an array for the days of the week, and a boolean to
represent whether it's on or off.
attr_accessor :hour, :minute, :second, :days, :active
Have a look at http://www.rubycentral.com/pickaxe/tut_classes.html
hth
alex
More information about the wxruby-users
mailing list