From: "Simon Kröger" Date: 2006-04-22T04:23:32+09:00 Subject: Re: Synchronized object Logan Capaldo wrote: > Well it's not quite as easy as Java but: > http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html > > > obj.methods.each do |meth| > (class << obj; self; end).module_eval < def #{meth}(*args, &block) > self.synchronize { super } > end > HERE > > end > > obj.extend(MonitorMixin) > > I did it in that order since I wouldn't want a synchronize { synchronize } accepted answer ... well kind of. ;) running your code (with an empty array) gave me: ruby 1.8.4 (2005-12-24) [i386-mswin32] (eval):1: warning: redefining `__send__' may cause serious problem (eval):1: warning: redefining `__id__' may cause serious problem (eval):2:in `extend': undefined method `synchronize' for # (NoMethodError) but i got the right idea i think. My code now looks like: --------------------------------------------------------------------- require 'monitor' def sync obj meth = obj.methods - %w{__send__ __id__} klass = class << obj.extend(MonitorMixin); self; end meth.each{|m|klass.module_eval "def #{m}(*a,&b)synchronize{super}end"} obj end --------------------------------------------------------------------- usage: --------------------------------------------------------------------- obj = sync([]) obj.push 'a' p obj --------------------------------------------------------------------- and now that i see it, it looks quite simple :) thanks to all of you who responded cheers Simon