Google Groups Home
Help | Sign in
Is there a better way to do this wxPython example in Ruby?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Wayne Magor  
View profile
 More options Jul 26, 11:16 pm
Newsgroups: comp.lang.ruby
From: Wayne Magor <wemag...@gmail.com>
Date: Sat, 26 Jul 2008 20:16:23 -0700 (PDT)
Local: Sat, Jul 26 2008 11:16 pm
Subject: Is there a better way to do this wxPython example in Ruby?
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Masover  
View profile
 More options Jul 26, 11:18 pm
Newsgroups: comp.lang.ruby
From: David Masover <ni...@slaphack.com>
Date: Sat, 26 Jul 2008 22:18:04 -0500
Local: Sat, Jul 26 2008 11:18 pm
Subject: Re: Is there a better way to do this wxPython example in Ruby?
On Saturday 26 July 2008 22:14:01 Wayne Magor 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..."}


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Is there a better way to do this wxPython example in Rub" by Joshua Ballanco
Joshua Ballanco  
View profile
 More options Jul 27, 8:20 pm
Newsgroups: comp.lang.ruby
From: Joshua Ballanco <jball...@gmail.com>
Date: Sun, 27 Jul 2008 19:20:30 -0500
Local: Sun, Jul 27 2008 8:20 pm
Subject: Re: Is there a better way to do this wxPython example in Rub

Wayne Magor 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
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Is there a better way to do this wxPython example in Ruby?" by Lars Christensen
Lars Christensen  
View profile
 More options Jul 28, 8:08 am
Newsgroups: comp.lang.ruby
From: Lars Christensen <lar...@belunktum.dk>
Date: Mon, 28 Jul 2008 05:08:38 -0700 (PDT)
Local: Mon, Jul 28 2008 8:08 am
Subject: Re: Is there a better way to do this wxPython example in Ruby?
On Jul 27, 5:16 am, Wayne Magor <wemag...@gmail.com> 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google