Cuando asignamos un valor a una variable no estamos copiando un objeto en esa variable, sólo estamos almacenando una referencia al objeto en esa variable:
>> person1 = "Tim" => "Tim" >> person1.class => String >> person1.object_id => 2150445000
>> person2 = person1 => "Tim" >> person1[0] = "J" => "J" >> person2 => "Jim"
El método dup puede ser usado para duplicar un objeto:
>> person2 = person1.dup => "Jim" >> person1[0] = "T" => "T" >> person2 => "Jim" >> person1 => "Tim"
Alternativamente podemos usar el método clone :
>> person2 = person1.clone => "Tim" >> person1[0] = "W" => "W" >> person2 => "Tim" >> person1 => "Wim"
Los objetos Fixnum son valores inmediatos, no son referencias. Son objetos inmutables.
[16:51][~/Dropbox/Public/LPP/1213(master)]$ irb ruby-1.9.2-p290 :001 > a = 2 => 2 ruby-1.9.2-p290 :002 > b = a => 2 ruby-1.9.2-p290 :003 > b = 5 => 5 ruby-1.9.2-p290 :004 > a => 2 ruby-1.9.2-p290 :005 > b => 5Los objetos Symbol son valores inmediatos, no son referencias. Son objetos inmutables.
ruby-1.9.2-p290 :006 > c = :z => :z ruby-1.9.2-p290 :007 > d = c => :z ruby-1.9.2-p290 :008 > d = :w => :w ruby-1.9.2-p290 :009 > c => :z ruby-1.9.2-p290 :010 > d => :w
Casiano Rodriguez León 2015-01-07