B
Brian Ross
[Note: parts of this message were removed to make it a legal post.]
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
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