How do I see an object's references in Ruby -
i'm trying understand ruby's subtle implementation of pass-by-value ... or pass-by-reference-by-value. makes sense when passing arguments method, when come across following, i'm little confused:
person = {name: "michael"} # => {:name => "michael"} person.object_id # => 70366412279760 full_name = person[:name] # => "michael" full_name.object_id # => 70366412279780 full_name << " jordan" # => "michael jordan" person # => {:name => "michael jordan"} how possible? how full_name contain reference person object? there method in ruby can "see" variable's references?
i've taken granted writing code above in past i'm trying further understand what's going on, i'm stumped.
full_name refers string object (which referred person[:name]). calling << on modifies string object rather reassigning variable named full_name.
full_name not carry information referent value :name in hash person. happens full_name , person[:name] point same object (id).
Comments
Post a Comment