From: "Peña, Botp" Date: 2008-08-15T15:56:01+09:00 Subject: Re: Use of dup to remove references From: Brian Ross [mailto:p.brian.ross@gmail.com] # def wipe_from( sentence ) # sentence_new = sentence if in doubt, insert p sentence.object_id p sentence_new.object_id # while sentence_new.include? '(' # open = sentence_new.index( '(' ) # close = sentence_new.index( ')', open ) # sentence_new[open..close] = '' if close # end # sentence_new # end # spoken = "I'm not happy with (nonsense) this moon." # test = wipe_from( spoken ) # puts spoken # puts test if in doubt, insert p spoken.object_id p test.object_id # # End of code # # I'd expect that the variable spoken would be unchanged because it's # sentence_new that's being modified. It works as expected if I variables are just references to objects. when you do y = "test" x = y x and y now references to the original string object "test" # use dup or clone, but why is spoken being modified at all? # Shouldn't it just be returning the value of sentence_new? not ruby. use dup or clone to taste :) anyway, in ruby nothing is impossible. In fact, there are many better ways ;) in your case eg, you can use string#gsub sample, irb(main):001:0> spoken = "I'm not happy with (nonsense) this moon." => "I'm not happy with (nonsense) this moon." irb(main):002:0> spoken_new = spoken.gsub(/\(.*\)/,"") => "I'm not happy with this moon." irb(main):003:0> spoken_new => "I'm not happy with this moon." irb(main):004:0> spoken => "I'm not happy with (nonsense) this moon." hth. kind regards -botp