18 Jul, 2009, Runter wrote in the 1st comment:
Votes: 0
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?

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.
18 Jul, 2009, David Haley wrote in the 2nd comment:
Votes: 0
Isn't it basically the same as passing in an anonymous function instead of a named function?
18 Jul, 2009, Runter wrote in the 3rd comment:
Votes: 0
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.
0.0/3