On Thu, Nov 22, 2012 at 8:17 AM, Garthy D <[email protected]> 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/ -- You received this message because you are subscribed to the Google Groups ruby-talk-google group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at https://groups.google.com/d/forum/ruby-talk-google?hl=en
