I use FxRuby to do some GUI programing. I define a method to handle the
same stuff if a button is either selected(SEL_COMMAND) or 'enter' key is
pressed (SEL_KEYPRESS).
Since the codes for each event are the same,I try to factor them out.
But I am not sure about the syntax. I wonder if someone can give me a
hand.
Thanks,
Li
###original codes###
def a_method @button_next.connect(SEL_COMMAND) do
# many lines for handling event A
...
...
···
#
end @button_next.connect(SEL_KEYPRESS) do
# many lines for handling event A
...
...
#
end
end
###### code formats after factoring
def a_method
if @button_next.connect(SEL_COMMAND)==true or @button_next.connect(SEL_KEYPRESS)==true do
# many lines for handling event A
...
...
#
end
end
--
Posted via http://www.ruby-forum.com/.
I use FxRuby to do some GUI programing. I define a method to handle the
same stuff if a button is either selected(SEL_COMMAND) or 'enter' key is
pressed (SEL_KEYPRESS).
Try something like this:
[SEL_COMMAND, SEL_KEYPRESS].each do |sel| @button_next.connect(sel) {...}
end
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
if @button_next.connect(SEL_COMMAND)==true or @button_next.connect(SEL_KEYPRESS)==true do
There is no need to compare equal to true. Just do this:
if @button_next.connect(SEL_COMMAND) or @button_next.connect(SEL_KEYPRESS) do
Of course, this is not the same if your connect method can return values other than `true` that are non-false/non-nil. However, if that is the case, it would be wise to reconsider the connect method to do the expected.
Alternatively, if this list could grow larger, you might consider something like this:
def a_method
sels = [SEL_COMMAND, SEL_KEYPRESS]
if sels.any? { |sel| @button_next.connect(sel) } do
# many lines for handling event A
...
···
#
end
end
Just extend `sels` to include the appropriate events/signals.
if @button_next.connect(SEL_COMMAND) or @button_next.connect(SEL_KEYPRESS) do
Alternatively, if this list could grow larger, you might consider
something like this:
def a_method
sels = [SEL_COMMAND, SEL_KEYPRESS]
if sels.any? { |sel| @button_next.connect(sel) } do
# many lines for handling event A
...
...
#
end
end
Just extend `sels` to include the appropriate events/signals.
Hi Matt,
Thanks. But none of them works for me.
I also try codes from Joel(another reply) and it works perfectly:
[SEL_COMMAND, SEL_KEYPRESS].each do |sel| @button_next.connect(sel) {...}
end
I could not figure out why your codes don't work for me.