From: Joel VanderWerf Date: 2009-05-16T04:13:33+09:00 Subject: Re: obj.attr(qualifier) = value -- possible? Michael Schuerig wrote: > I want to write a method that can be called like this > > obj.attr(qualifier) = value > > So far, I don't see how to achieve this. I've tried > > class C > def attr=(qualifier, value) > ... > end > end > > as well as > > class D > def attr(qualifier) > Proxy.new(self, qualifier) > end > class Proxy > def =(value) > ... > end > end > end > > Neither is valid Ruby, apparently. Is there another, working way? > > Michael > I'm pretty sure that's impossible -- there is no #() method, or #()=. But there is #[]=, and you can adapt your Proxy approach to use it: class D def attr Proxy.new(self) end def handle qualifier, value puts "handling #{qualifier.inspect}, #{value.inspect}" end class Proxy def initialize obj @obj = obj end def []=(qualifier, value) @obj.send(:handle, qualifier, value) end end end d = D.new d.attr["some qualifier"] = "some value" -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407