# Events?

**URL:** <https://www.ruby-forum.com/t/events/145899>\
**Category:** IronRuby\
**Created:** [August 14, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/events/145899 "2008-08-14T21:11:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 14, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/events/145899/1 "2008-08-14T21:11:05Z")

</div>

Hi all,  
Is there a built-in event system for IronRuby? I know I can handle  
events  
with button.click { }, but what if I want to dispatch my own, custom  
events  
(from a class I created myself?).

I wrote my own event system, but I would prefer to use a built-in  
dispatch  
method if it exists.

Thanks!  
~sean

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 14, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/events/145899/2 "2008-08-14T22:23:16Z")

</div>

I’m not sure I understand the scenario. Would you like to declare an  
event on Ruby object that you want to hook from C# code? Like  
rubyEngine.Operations.GetMember(obj, “my\_event”) += delegate { …}?

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Sean C.  
Hess  
Sent: Thursday, August 14, 2008 12:04 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Events?

Hi all,

Is there a built-in event system for IronRuby? I know I can handle  
events with button.click { }, but what if I want to dispatch my own,  
custom events (from a class I created myself?).

I wrote my own event system, but I would prefer to use a built-in  
dispatch method if it exists.

Thanks!  
~sean

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 14, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/events/145899/3 "2008-08-14T22:29:08Z")

</div>

Let’s say I create a class called MovieControls, that wraps some  
existing  
xaml. The xaml has several buttons, including a toggle play/pause  
button,  
and a previous and next button.  
MovieControls would listen to the click of the play/pause button, and  
would  
dispatch a “play” event when they clicked it to play it, and “pause” for  
the  
opposite.

I want to be able to listen to MovieControls to know when to tell my  
video  
to “play” and “pause”.

So, it would be something like this… In MovieControls  
play\_pause\_button.click { if (isPlayState) dispatch(“play”) else  
dispatch(“pause”) end }

Then, in my app  
myMovieControls.play { doPlay }  
myMovieControls.pause { doPause }

Does that make more sense now? If this seems totally out of the blue,  
how  
are people currently handling abstracting away this kind of  
functionality?

Thanks  
~sean

On Thu, Aug 14, 2008 at 2:22 PM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 15, 2008, 12:17am UTC](https://www.ruby-forum.com/t/events/145899/4 "2008-08-15T00:17:34Z")

</div>

Cool… that’s pretty much what I did. Thanks  
On Thu, Aug 14, 2008 at 4:08 PM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 15, 2008, 12:09am UTC](https://www.ruby-forum.com/t/events/145899/5 "2008-08-15T00:09:57Z")

</div>

It seems that both parts of the code are Ruby. So you should use Ruby  
language features. It’s possible to emulate CLR events by a list of  
blocks.

event = []

# adding a handler:

handler = lambda { … }  
event \<\< handler

# removing a handler:

event.delete handler

# firing an event:

event.each { |handler| handler[args] }

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Sean C.  
Hess  
Sent: Thursday, August 14, 2008 1:29 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Events?

Let’s say I create a class called MovieControls, that wraps some  
existing xaml. The xaml has several buttons, including a toggle  
play/pause button, and a previous and next button.

MovieControls would listen to the click of the play/pause button, and  
would dispatch a “play” event when they clicked it to play it, and  
“pause” for the opposite.

I want to be able to listen to MovieControls to know when to tell my  
video to “play” and “pause”.

So, it would be something like this… In MovieControls  
play\_pause\_button.click { if (isPlayState) dispatch(“play”) else  
dispatch(“pause”) end }

Then, in my app  
myMovieControls.play { doPlay }  
myMovieControls.pause { doPause }

Does that make more sense now? If this seems totally out of the blue,  
how are people currently handling abstracting away this kind of  
functionality?

Thanks  
~sean

On Thu, Aug 14, 2008 at 2:22 PM, Tomas M.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:

I’m not sure I understand the scenario. Would you like to declare an  
event on Ruby object that you want to hook from C# code? Like  
rubyEngine.Operations.GetMember(obj, “my\_event”) += delegate { …}?

Tomas

From:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
On Behalf Of Sean C. Hess  
Sent: Thursday, August 14, 2008 12:04 PM  
To: removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [Ironruby-core] Events?

Hi all,

Is there a built-in event system for IronRuby? I know I can handle  
events with button.click { }, but what if I want to dispatch my own,  
custom events (from a class I created myself?).

I wrote my own event system, but I would prefer to use a built-in  
dispatch method if it exists.

Thanks!

~sean

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 15, 2008, 8:28am UTC](https://www.ruby-forum.com/t/events/145899/6 "2008-08-15T08:28:51Z")

</div>

I actually use a customized version of Observable that includes the  
sender  
of the event.  
It’s that approach but a little bit more formalized. It’s in the  
IronNails  
framework  
[http://github.com/casualjim/ironnails/tree/master/IronNails/vendor/iron\_nails/lib/observable.rb](http://github.com/casualjim/ironnails/tree/master/IronNails/vendor/iron_nails/lib/observable.rb)  
That should look a lot like how events are implemented in C#.

2008/8/15 Sean C. Hess [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 15, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/events/145899/7 "2008-08-15T19:27:23Z")

</div>

Here is another way to do it:

- In the XAML add commands to the appropriate buttons. You can use  
MediaCommands - Play, Pause, Stop, etc.
- Have something like a ViewModel that holds the logic for what  
should  
happen when each command get executed.
- Somehow add/attach/inject the view model to the view and create  
CommandBinding foreach of the supported commands. Add this command  
bindings  
to some visual element from the XAML, maybe the root.

Regards,  
Stefan

2008/8/15 Sean C. Hess [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Sean\_CSHess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/94ad74/32.png) [@Sean\_CSHess](https://www.ruby-forum.com/u/Sean_CSHess)\
**Post date:** [August 15, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/events/145899/8 "2008-08-15T14:13:07Z")

</div>

Awesome, thanks for the example Ivan. It’s good to see it done  
“correctly”  
😜 Actually, mine includes the sender of the event and an event object  
to,  
I just wasn’t paying attention to them in the handler example, but yours  
is  
definitely more formal. Thanks again
