Is Ruby pass by reference or by value? -
@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 lang_errors
variable in update_lanugages
method. when perform save on @user
object lose errors stored in lang_errors
variable.
though attempting more of hack (which not seem working). understand why variable values washed out. understand pass reference know how value can held in variable without being washed out.
in traditional terminology, ruby strictly pass-by-value. that's not you're asking here.
ruby doesn't have concept of pure, non-reference value, can't pass 1 method. variables references objects. in order object won't change out under you, need dup or clone object you're passed, giving object nobody else has reference to. (even isn't bulletproof, though — both of standard cloning methods shallow copy, instance variables of clone still point same objects originals did. if objects referenced ivars mutate, still show in copy, since it's referencing same objects.)
Comments
Post a Comment