I've been doing a lot of event-driven programming in ruby lately, and it's involved a lot of repeating to generate methods for each and every event my application can fire off, and I can only support one handler per event with my current methods. Since I'm a big believer in DRY (Don't Repeat Yourself), and I was also spoiled by the excellent event handling in C# and Vala (Gobject, really), I decided to write something similar to ruby. This, is ruby-event.
So, while this code is already useful to me, I figured I'd make an example to show why I wrote this in the first place. If you remember a while back, I wrote a guide on threaded networking in ruby with a cute little wrapper clase that called a block each time a line was recieved. This right here is a prime example of event driven programming, so I decided to rewrite some of my code to make use of ruby-event.
By using ruby-event, I jut removed three lines from my code and made it much more usable (and it's a lot cleaner too). Now, imagine that savings added up by dozens of events you use throughout your program, that's a lot of code you saved yourself from writing. However, how do you actually bind a handler to an event? Well, here's how.
That's it, you just 'add' a new Proc object to the event and you're done. You can assign as many handlers as you like for each event, and they will all be called in order they were assigned.
Also, see how the object that fired the event is passed as the first argument to your block (I usually call this sender)? This allows you to create one proc object to handle an event and assign it to multiple objects that need it.
Well, I think that about wraps it up for today's ruby on snuxoll, have fun!

Comments
gucci
Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum! http://www.oneor-more.com/
Post new comment