# Event syntax

**URL:** <https://www.ruby-forum.com/t/event-syntax/202770>\
**Category:** wxRuby\
**Created:** [February 5, 2011, 2:34pm UTC](https://www.ruby-forum.com/t/event-syntax/202770 "2011-02-05T14:34:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 5, 2011, 2:34pm UTC](https://www.ruby-forum.com/t/event-syntax/202770/1 "2011-02-05T14:34:28Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jkadarusman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@jkadarusman](https://www.ruby-forum.com/u/jkadarusman)\
**Post date:** [February 6, 2011, 12:18am UTC](https://www.ruby-forum.com/t/event-syntax/202770/2 "2011-02-06T00:18:39Z")

</div>

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
