Forum: wxRuby Event syntax

B23440b2d6c0c937c5917e13631f5614?d=identicon&s=25 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!
06f6780c99d4a8dd71f2b474082ea9ce?d=identicon&s=25 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
This topic is locked and can not be replied to.