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

Tom Stuart tom at experthuman.com
Wed Sep 21 03:03:23 PDT 2011


On 21 Sep 2011, at 10:57, Tom Stuart wrote:
> You do just have to say "self.configuration =" if you want Ruby to treat it as a call to #configuration= rather than an assignment to a local variable called "configuration". This is a quirk of the way Ruby decides, syntactically, what the local variables are (roughly: by looking for things that look like assignments).

To unpack that a bit: when Ruby sees the second mention of "configuration", it needs to decide whether this is a call to a method called #configuration or a use of a local variable called configuration, and it makes that decision by just looking to see whether there is a local variable of that name or not. But the first mention ("configuration =") is enough to make it think that there is a local variable of that name, so it treats it as such rather than a method call; if you use "self.configuration =" then it won't think there's a local variable of that name, so the second mention will result in a method call as intended.

Cheers,
-Tom


More information about the Chat mailing list