Event syntax

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!

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs