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:
static VALUE get_content(VALUE self) {
xmlNodePtr node;
xmlChar * content;
Data_Get_Struct(self, xmlNode, node);
content = xmlNodeGetContent(node);
if(content) {
VALUE rval = NOKOGIRI_STR_NEW2(content);
xmlFree(content);
return rval;
}
return Qnil;
}
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:
def content= string
self.native_content = encode_special_chars(string.to_s)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With