Question

What is the difference between

node.property("name")

and

node.attributes["name"]

According to documentation one returns "named property value" and the other "attribute value", but I do not see any difference.

One thing which might play some role are namespaces. Both methods have their setter versions node.property("name")=value and node.attributes["name"]=value and there might be a difference how they treat namespaced attributes.

Was it helpful?

Solution

Node#property, view source:

# File lib/libxml/properties.rb, line 5
def property(name)
  warn('Node#properties is deprecated.  Use Node#[] instead.')
  self[name]
end

So your question becomes what's the difference between Node#[] and Node#attributes. The answer is that Node#[] returns a single attribute, and Node#attributes returns a hash containing all the attributes, which is easier than retrieving them one at a time. Of course, you can do a lookup into any hash by writing ['some_key'] after the hash, e.g.:

puts( 
  { a: 1, b: 2}[:b] 
)

Node#[] is a more efficient way to look up one attribute because it doesn't create the whole hash first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top