@user.update_languages(params[:language][:language1],
params[:language][:language2],
params[:language][:language3])
lang_errors = @user.errors
logger.debug "--------------------LANG_ERRORS----------101-------------"
+ lang_errors.full_messages.inspect
if params[:user]
@user.state = params[:user][:state]
success = success & @user.save
end
logger.debug "--------------------LANG_ERRORS-------------102----------"
+ lang_errors.full_messages.inspect
if lang_errors.full_messages.empty?
@user
object adds errors to the lang_errors
variable in the update_lanugages
method.
when I perform a save on the @user
object I lose the errors that were initially stored in the lang_errors
variable.
Though what I am attempting to do would be more of a hack (which does not seem to be working). I would like to understand why the variable values are washed out. I understand pass by reference so I would like to know how the value can be held in that variable without being washed out.
Best Answer
The other answerers are all correct, but a friend asked me to explain this to him and what it really boils down to is how Ruby handles variables, so I thought I would share some simple pictures / explanations I wrote for him (apologies for the length and probably some oversimplification):
Q1: What happens when you assign a new variable
str
to a value of'foo'
?A: A label called
str
is created that points at the object'foo'
, which for the state of this Ruby interpreter happens to be at memory location2000
.Q2: What happens when you assign the existing variable
str
to a new object using=
?A: The label
str
now points to a different object.Q3: What happens when you assign a new variable
=
tostr
?A: A new label called
str2
is created that points at the same object asstr
.Q4: What happens if the object referenced by
str
andstr2
gets changed?A: Both labels still point at the same object, but that object itself has mutated (its contents have changed to be something else).
How does this relate to the original question?
It's basically the same as what happens in Q3/Q4; the method gets its own private copy of the variable / label (
str2
) that gets passed in to it (str
). It can't change which object the labelstr
points to, but it can change the contents of the object that they both reference to contain else: