From: Brian Ross
Date: 2008-08-15T10:41:01+09:00
Subject: Use of dup to remove references
------=_Part_34945_1127406.1218764630041
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Hello
I am new to ruby and looking for a brief explanation on why a code sample
works in an unexpected way.
# Beginning of code
def wipe_from( sentence )
sentence_new = sentence
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
# 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 use dup or
clone, but why is spoken being modified at all? Shouldn't it just be
returning the value of sentence_new?
Brian
------=_Part_34945_1127406.1218764630041--