# Code factoring problem

**URL:** <https://www.ruby-forum.com/t/code-factoring-problem/157254>\
**Category:** Ruby\
**Created:** [January 8, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/code-factoring-problem/157254 "2009-01-08T22:02:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 8, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/code-factoring-problem/157254/1 "2009-01-08T22:02:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 8, 2009, 10:49pm UTC](https://www.ruby-forum.com/t/code-factoring-problem/157254/2 "2009-01-08T22:49:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 8, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/code-factoring-problem/157254/3 "2009-01-08T22:56:54Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 9, 2009, 1:42am UTC](https://www.ruby-forum.com/t/code-factoring-problem/157254/4 "2009-01-09T01:42:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 9, 2009, 1:41am UTC](https://www.ruby-forum.com/t/code-factoring-problem/157254/5 "2009-01-09T01:41:42Z")

</div>

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
