From: Jordi Date: 2007-11-08T18:08:04+09:00 Subject: Proposal of a new operator for Method and Proc Hello, this email is long but I hope you to read it. I think it is worth it. I think I found a powerful new operator for ruby that can make the language even better. If you want to just read about that go to ==The cool stuff== section below. == The search for events == First, the classes below are not tested and I'm just a beginner, some errors are took for granted. Won't affect my discourse. I wanted to implement a timer, looking in google the first thing you get is something like this: class Timer def start (timeout) th = Thread.new do loop do # work done by the timer sleep(timeout) end end end end Let's suppose I'm only using the timer to implement some kind of counter. Cool, BUT I would like that the work of the timer be only time related! and the counter be in its relevant class. Also I may want to reuse this class. So I thought about something like this class Timer def initialize (*arguments) connections=[] @arguments=arguments end def register_work ( pointer) connections += pointer end def deregister_work (pointer) connections -= pointer end def deregister_all connections =[] end def list connections.each { |l| puts "#{l}" } end def start (timeout) th = Thread.new do loop do # work done by the timer connections.each { |l| l.call(*@arguments) } sleep (timeout) end end end end I wrote pointer, I meant for instance Counter.count() This class is much more general and powerful. The work is done in the relevant classes and not inside the timer Also, we can register as many connected classes as we want. Seeing that this is Good (tm) I thought about how can I make any class use this. Of course I can make a class with the register, deregister and call thing and mixin it. But Wouldn't be could that these encapsulated connections are supported natively by ruby? About benefits of this scheme: http://doc.trolltech.com/4.3/signalsandslots.html About a comparison of implementations (going straight to the comparison table is better): http://scottcollins.net/articles/a-deeper-look-at-signals-and-slots.html ==The cool stuff == I thought about how to get this feature implemented without polluting the most basic class of Ruby. I think that the most powerful and less polluting way is adding a new operator to Method and Proc. Example. I have a class Page, a class Timer and a class Counter. The Page instance is the parent of both instances of Timer and Counter. class Page def initialize counter = Counter.new t=Timer.new t.timeout -> counter.update counter.overrun -> show_error end end Some notes: We connect the timeout of the timer with the update of the counter. We connect the overrun of the counter with an error show by the page. The Timer whenever it timeouts, it just ... call to its .timeout() method. Note that the Timer class does not need to be modified to make something new (update the counter) . It is a real isolated software component. The same goes to the counter, it overruns and let the world know about it. That is its job. Don't need to know about the Page having and error. I proposed "->" in my ignorance. It may appear as a redirect rather than "something you add". Surely something else is better. With this scheme, we have a general and easy , cool, even rubyish way to connect to events and to connect code arbitrarily to any class method! For instance, we need to make some clean up each time we call to File.close() (whatever reason) Previous simple way def close (file) file.close @filesOpened -= 1 end new way (in the construstor): file.close -> { @filesOpened -= 1 } { @filesOpened -= 1 } will become a Proc called when file.close finish. See that the implementation in C should be efficient (a list of pointers to functions) Now suppose that you load a new plugin that also need to do some cleanup when the file open by the main class is closed. When loaded it will add itself whatever it needs for itself. i feel my examples are not very good... sorry. == Summary == This addition to the language isolates software components and creates a new very powerful construction at a very low cost. -- Jordi Polo (ジョルデイ ポロ) 奈良先端科学技術大学院大学 情報科学研究科 自然言語処理学講座 D1