# Event handling

**URL:** <https://www.ruby-forum.com/t/event-handling/136840>\
**Category:** Ruby\
**Created:** [May 6, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/event-handling/136840 "2008-05-06T19:33:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 6, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/event-handling/136840/1 "2008-05-06T19:33:50Z")

</div>

Hi,

Does ruby has some standard event handling functionality? I’ve searched  
google and the “Programming Ruby book” but it appears there isn’t much  
info on event handling, so maybe it is named differently in ruby?

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 8, 2008, 7:42am UTC](https://www.ruby-forum.com/t/event-handling/136840/2 "2008-05-08T07:42:09Z")

</div>

Oke, I’ve implemented the following observer/listener program:

class Cow  
def initialize  
@listeners = Array.new  
end

def add\_listener(listener)  
raise ArgumentError if !listener.respond\_to?(:sound\_event) &&  
!listeners.include?(listener)  
@listeners \<\< listener  
end

def remove\_listener(listener)  
@listeners.delete(listener)  
end

def notify\_listeners  
for i in removed\_email\_address@domain.invalid  
@listeners[i].sound\_event(‘MHOEEEE’)  
end  
end

def be\_happy  
notify\_listeners  
end  
end

class Program  
def sound\_event(value)  
puts value  
end

def start  
cow = Cow.new  
cow.add\_listener(self)  
cow.be\_happy  
end  
end

Program.new.start

Is this an appropriate way of doing event handling in ruby?

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 8, 2008, 9:11am UTC](https://www.ruby-forum.com/t/event-handling/136840/3 "2008-05-08T09:11:57Z")

</div>

This seems like a decent approach; maintaining a list of listeners and  
notifying them. A few notes:  
for i in removed\_email\_address@domain.invalid  
@listeners[i].sound\_event(‘MHOEEEE’)  
end

Closures or blocks in ruby are perfect for this:  
@listeners.each do |listener|  
listener.sound\_event ‘MHOEEEE’  
end

to get cool eventing like:

@car.when :crashing do  
jump\_out  
end  
I recommend looking at Publisher:  
[http://rubyforge.org/projects/atomicobjectrb/](http://rubyforge.org/projects/atomicobjectrb/)

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 8, 2008, 10:26am UTC](https://www.ruby-forum.com/t/event-handling/136840/4 "2008-05-08T10:26:50Z")

</div>

Hello,

You can also have listeners register blocks to certain events instead  
of implementing a specific method like “sound\_event”. That way you  
publisher can be more decoupled from the listeners’ implementation.  
Have a look at [http://nutrun.com/weblog/event-registry/](http://nutrun.com/weblog/event-registry/)

Thanks,  
George

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 8, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/event-handling/136840/5 "2008-05-08T18:49:54Z")

</div>

Thanks, I’ll check both of the suggested techniques.
