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. 


On Thursday, 22 November 2012 at 6:55 PM, Robert Klemme wrote:

> On Thu, Nov 22, 2012 at 8:17 AM, Garthy D
> <garthy_lmkltybr / entropicsoftware.com (mailto:garthy_lmkltybr / entropicsoftware.com)> wrote:
> 
> > What I don't like with the above is that whenever I set v, I have to be
> > careful to use "self.v = <value>", rather than "v = <value>" when I perform
> > assignment, because the latter will just create a local variable and not
> > call the "v=" method I've created. It's fine when called externally on the
> > object, but any internal writes need a "self." prefix. I'm not keen on
> > expecting the user to do this each time, when using "@v" equivalent is so
> > much easier to use.
> > 
> > Is there a better way to implement this, such that the user can write code
> > similarly to the listing at the top? Is there some syntactic sugar I am
> > unaware of that can be used instead of "self.foo"? The goal is to make the
> > user API for this nice and simple- I don't mind if I have to mess about to
> > make it happen, and I've already got a custom "method_missing", and adding
> > code to that is no issue.
> 
> 
> 
> The only solution for this that I can see is to use some kind of
> transaction: you open the transaction, store object state, work with
> the object and at the end something inspects instance variables and
> writes all changed values. But frankly, OR mappers do exist for Ruby,
> ActiveRecord is just one of them. So if it is OK to fill in "RDBMS"
> for "remote storage" then you could simply use that. Other than that
> I'd stick with using the accessor.
> 
> Kind regards
> 
> robert
> 
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/