# Is there a better way to do this wxPython example in Ruby?

**URL:** <https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-wxpython-example-in-ruby/144234>\
**Category:** Ruby\
**Created:** [July 27, 2008, 5:20am UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-wxpython-example-in-ruby/144234 "2008-07-27T05:20:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 27, 2008, 5:20am UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-wxpython-example-in-ruby/144234/1 "2008-07-27T05:20:54Z")

</div>

I have been translating examples from the book “wxPython in Action”  
into Ruby using wxRuby2. I ran into one example where a method was  
passed as a parameter in Python. I came up with the solution shown  
below in Ruby (which works fine) but it seems ugly to me. If someone  
was not very familiar with Ruby they would say “hmmm… what’s this  
ampersand lambda stuff”.

Is there a better way to do this in Ruby?

Python example using wxPython:

```
def createButtonBar(self, panel):
    self.buildOneButton(panel, "FIRST", self.OnFirst)
    self.buildOneButton(panel, "<< PREV", self.OnPrev, (80, 0))
    self.buildOneButton(panel, "NEXT >>", self.OnNext, (160, 0))
    self.buildOneButton(panel, "LAST", self.OnLast, (240, 0))

def buildOneButton(self, parent, label, handler, pos=(0,0)):
    button = wx.Button(parent, -1, label, pos)
    self.Bind(wx.EVT_BUTTON, handler, button)
    return button

```

My translation to Ruby and wxRuby2:

def create\_button\_bar(panel)  
build\_one\_button(panel, “FIRST”, &lambda {|e|  
on\_first(e)})  
build\_one\_button(panel, “\<\< PREV”, [80, 0], &lambda {|e|  
on\_prev(e)})  
build\_one\_button(panel, “NEXT \>\>”, [160, 0], &lambda {|e|  
on\_next(e)})  
build\_one\_button(panel, “LAST”, [240, 0], &lambda {|e|  
on\_last(e)})  
end

def build\_one\_button(parent, label, pos=[0,0], &handler)  
button = Wx::Button.new(parent, -1, label, pos)  
evt\_button(button) {|event| yield(event)}  
return button  
end

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 27, 2008, 5:25am UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-wxpython-example-in-ruby/144234/2 "2008-07-27T05:25:00Z")

</div>

On Saturday 26 July 2008 22:14:01 Wayne M. wrote:

> ```
> build_one_button(panel, "LAST", [240, 0], &lambda {|e|
> 
> ```
> 
> on\_last(e)})

> def build\_one\_button(parent, label, pos=[0,0], &handler)

Looks like, at the very least, you could do:

build\_one\_button(panel, “LAST”, [240,0]) {|e| on\_last(e)}

I think your function is already set up to accept a block, so it’s the  
same  
syntax as each(), which you’re probably already familiar with:

(1…99).each {|i| puts “#{i} bottles of beer on the wall…”}

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 28, 2008, 2:20am UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-wxpython-example-in-ruby/144234/3 "2008-07-28T02:20:49Z")

</div>

Wayne M. wrote:

> Is there a better way to do this in Ruby?

What about passing the method name as a Symbol and then using  
Object.send in the reciever? Something like:

build\_one\_button(panel, “FIRST”, :on\_first)

def build\_one\_button(parent, label, pos=[0,0], method\_name)  
button = Wx::Button.new(parent, -1, label, pos)  
evt\_button(button) {|event| self.send(method\_name, event)}  
return button  
end

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 28, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/is-there-a-better-way-to-do-this-wxpython-example-in-ruby/144234/4 "2008-07-28T14:13:37Z")

</div>

On Jul 27, 5:16 am, Wayne M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Python example using wxPython:
> 
> ```
> self.buildOneButton(panel, "FIRST", self.OnFirst)
> 
> ```

I think the most direct translation of the Python code would be:

build\_one\_button(panel, “FIRST”, method(:on\_first))

Simpler than the block wrapper, IMO, and more efficient.

Lars
