MudBytes
» MUDBytes Community » Language Discussions » Ruby » Observer pattern
Pages: << prev 1 next >>
Observer pattern
Runter
Wizard






Group: Members
Posts: 1,850
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#1 id:29110 Posted Jul 18, 2009, 1:01 am

After the discussion about using observer on another thread I wrote a little code to let me use blocks/closures.  Was wondering if anybody else had done this and had an insight on its actual application?

Code (text):
1
2
3
4
5
6
7
 
obj.add_observer(:type_selected) do |data|
  ### do something with data variable
end
 


Basically the code block is doing the observing and will be executed.  :type_selected is the filter so it doesn't receive all types of data.  Seems interesting, although I think it may just have the same (or even less) usefulness.  (On a side note, though, it isn't really much different except I can attached loaded script directly to an event.)

edit:  There's a standard library implementing the pattern in Ruby, but it doesn't allow blocks as observers.
.........................
CoralMud project

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Last edited Jul 18, 2009, 1:18 am by Runter
David Haley
Wizard






Group: Members
Posts: 6,874
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#2 id:29136 Posted Jul 18, 2009, 10:31 am

Isn't it basically the same as passing in an anonymous function instead of a named function?
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Runter
Wizard






Group: Members
Posts: 1,850
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#3 id:29144 Posted Jul 18, 2009, 2:11 pm


David Haley said:
Isn't it basically the same as passing in an anonymous function instead of a named function?


I suppose.  The block itself is an object so there's really not much difference between it and any other object as an observer.  Interestingly, it can return values back to the observed, though.
.........................
CoralMud project

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Pages:<< prev 1 next >>
Tags
[+]

Valid XHTML 1.1! Valid CSS!