Forum: Ruby code factoring problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Li C. (Guest)
on 2009-01-08 23:02
Hi all,

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
Joel VanderWerf (Guest)
on 2009-01-08 23:49
(Received via mailing list)
Li Chen wrote:
> Hi all,
>
> 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
Matthew M. (Guest)
on 2009-01-08 23:56
(Received via mailing list)
>
>  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.
Li C. (Guest)
on 2009-01-09 02:41
Matthew M. wrote:
>Just do this:
>
>      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.

Li
Li C. (Guest)
on 2009-01-09 02:42
Joel VanderWerf wrote:
> Try something like this:
>
>    [SEL_COMMAND, SEL_KEYPRESS].each do |sel|
>      @button_next.connect(sel) {...}
>    end

Thanks Joel. They work very well.

Li
This topic is locked and can not be replied to.