Forum: wxRuby Event syntax

Posted by Jason Knott (optictygre)
on 2011-02-05 14:34
Class A is a ruby file generated from XRC via xrcise.  Class B is a
child class that inherits from class A, and subsequently is where I have
my event hook-ups defined (just in case I need to generate class A from
XRC again, I don't want the event code overwritten).

My question is about the syntax for the event hookups.  Why does the
code below work the way it does?

class B < A

def initialize
  super
  evt_menu(@m_menuAbout) { | event | onAbout(event) } #this works
  evt_menu(self.m_menuAbout) { | event | onAbout2(event) } #also works
  evt_menu(@m_menuAbout) { :onAbout3 } #does not work
end

def onAbout(event)
  puts 'You clicked the About menu'
end

def onAbout2(event)
  puts 'You clicked the About menu'
end

def onAbout3
  puts 'You would have cliked the About menu but the code doesn't get
here'
end

end

Kind of new to ruby, so I'm not sure why I need to include the | event |
syntax for the event hookup.  Any explainations?  Thanks!
Posted by Alex Fenton (Guest)
on 2011-02-06 00:18
(Received via mailing list)
Hi Jason

On 05/02/11 13:34, Jason Knott wrote:
> def initialize
>    super
>    evt_menu(@m_menuAbout) { | event | onAbout(event) } #this works
>    evt_menu(self.m_menuAbout) { | event | onAbout2(event) } #also works
>    evt_menu(@m_menuAbout) { :onAbout3 } #does not work
> end

Look at this code just from a general Ruby point of view. Your last
example is a block / anonymous function that simply returns the symbol
value :onAbout3. It is running (I'm pretty sure) but not doing anything
interesting.

I think what you want is evt_menu(@m_menuAbout, :onAbout3). Then the
symbol is interpreted immediately by evt_menu, which takes it to mean
"bind the method named "..." to the event handler". The bound method can
accept an event argument, or not, as it pleases.

best
alex
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.