From: Garthy D Date: 2012-11-22T19:18:01+09:00 Subject: Re: Instance-variable-like user API Hi Arlen, On 22/11/12 20:25, Arlen Cuss wrote: > If you don't mind them accessing all vars through a name, you could have an API like: > > class A< SomeBase > def initialize > super > remote[:v] = 1 > end > > def inc > remote[:v] += 1 > end > > def magic > do_something(remote[:v]) > end > end > > Or: > > class A< SomeBase > def initialize > super > remote.v = 1 > end > > def inc > remote.v += 1 > end > > def magic > do_something(remote.v) > end > end > > > It makes it a bit more obvious what's happening. The other benefit of this method is that it's not required to declare these with magic_accessor, as whatever #remote returns can deal magically with #[]/#[]= or #method_missing. Thanks for putting that together, it's a good solution- and actually very close to what I currently have implemented. :) I have a dedicated remote object with "[]" and "[]=" to handle arbitrary variables, and as you say, there's no need to declare anything with magic_accessor. It all happens like magic. I'd also considered your second suggestion as a possible solution too. It's reassuring that people are thinking along the same lines. :) Unfortunately, in use I found the solution a bit awkward. It's perfectly fine for simpler methods, but it gets a bit unwieldy when doing anything complex. At this point I set out to improve it, and ran into the "self." shortcoming. At that point I realised I didn't actually know of a nice way to improve it without some sort of downside, so I thought I'd post to the list to get a second, third, and fourth opinion, in case I'd missed something. :} Cheers, Garth