Could someone please explain the difference between the following two lines of code:
1. element.content.gsub!("#{i}", "#{a[i]}")
2. element.content = element.content.gsub("#{i}", "#{a[i]}")
In the following code:
a.each_index do |i|
@doc.traverse do |element|
if element.text?
element.content = element.content.gsub("#{i}", "#{a[i]}")
end
end
end
puts @doc
The code as presented above does change @doc. While if I use line 1 with gsub! it doesn’t have an effect on @doc. Does this have to do with how blocks handle their parameters? Shouldn’t everything be passed by reference in Ruby unless explicitly copied using a method?
Checking http://nokogiri.org/Nokogiri/XML/Node.html:
A copy of the content is made, so any changes to it only affect that copy, and not the internal value of the node’s contents.
Using ‘element.content=’ calls a separate method that does modify the internal value: