[LRUG] Unexpected behaviour of << operator
Marcus Roberts
marcus at marcusr.org.uk
Wed Aug 18 08:15:20 PDT 2010
I've just been chasing down a weird bug, the cause of which was accidentally using << instead of =
But this led me to try and work out how an ActiveRecord object field was updating
A simple example:
>> p = Project.find 1
=> #<Project id: 1, user_id: 1, name: "A project name", ...>
>> n = p.name
=> "A project name"
>> n += "_extra"
=> "A project name_extra"
>> p.name
=> "A project name"
but
>> p = Project.find 1
=> #<Project id: 1, user_id: 1, name: "A project name", ...>
>> n = p.name
=> "A project name"
>> n << "_extra"
=> "A project name_extra"
>> p.name
=> "A project name_extra"
I suspect this is something to do with clever overloading of << or my complete mis-understanding of something fundamental, but does anyone know why the change gets passed back through when I expected the original variable n to be a value, and not a reference
Marcus
More information about the Chat
mailing list