[LRUG] attr_accessor parsed as a method variable when setting with a get

Paul Robinson paul at 32moves.com
Wed Sep 21 04:04:23 PDT 2011


On 21 Sep 2011, at 11:31, Tom Stuart wrote:

> A question of taste. If I've defined public accessors, my preference is to use them even within methods of that object, rather than touching the instance variable directly. I like this because a) it encapsulates access to that piece of state, in case I want to provide a default or otherwise wrap reads/writes to the instance variable


A wrapper is useful if you think there is going to be more stuff going on and you want to "guard" your instance variable or do funky things with it and it's going to be accessed all over the place, but otherwise it seems a bit ugly to my eye for simpler cases.


> , and b) instance variables are ugly innit.


I think instance variables are pretty elegant in some cases. Rails would be significantly less elegant without them. :-)


> But if you like reading/writing instance variables in your methods, perhaps for Nebulous Performance Reasons, that's fine too.


Agreed, matter of taste. In that (trivial) example though, I'd have gone for @configuration over self.configuration unless I expected I needed to protect/guard/funk up what happened to "configuration" and there were many places it could be set.


More information about the Chat mailing list