0

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.

gorn
  • 5,042
  • 7
  • 31
  • 46

1 Answers1

1

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.

7stud
  • 46,922
  • 14
  • 101
  • 127
  • So basically attribites and (named) properties are the same thing. Do you know where I can submit correction to [this documentation](https://xml4r.github.io/libxml-ruby/rdoc/index.html)? It is quite confusing. – gorn Jul 29 '14 at 23:55
  • Does not Node#attributes return an XML::Attributes object? – gorn Jul 29 '14 at 23:56
  • @gorn, Take a look at the source code for Node#property, and look at the variable names. The ruby docs are the worst in the business. Good luck getting them changed. If you get too frustrated, one option is to abandon ruby and take up python or perl. – 7stud Jul 30 '14 at 00:23
  • @gorn: Yes, in fact the docs say that Node#attributes returns an XML::Attributes object, and if you look at the docs for XML::Attribute here: https://xml4r.github.io/libxml-ruby/rdoc/index.html, that object has the same getter and setter methods as a Hash, so it is Hash-like. A Hash is just an object too. The point is that the thing returned by Node#attributes is a *collection* of all the attributes. Whether the collection is a Hash object, an Array object, or an XML::Attributes object is irrelevant to your question. – 7stud Jul 30 '14 at 00:29
  • first of all thanks for very good answer. Second - I am little confused where I can help with docs - this thing i see at https://xml4r.github.io/libxml-ruby/rdoc/index.html has no obvious connection to source code at https://github.com/xml4r/libxml-ruby/ ... can you point me in correct directions? BTW I am coming from perl word and both of them have their merits and pitfalls :) – gorn Jul 30 '14 at 00:37
  • 1
    @gorn, At the first link, if you look in the middle pane on the left for `LibXML::XML::Node`, then click on it, then scroll down to the Public Instance Methods and look for the entry `node.property("name") -> "string"`, there is a link that says *Show source*. Click on that, and you will see that all the names in the C code have `attribute` in them, and nowhere will you see a variable named `property`. And...perl's docs are pretty darn good, and perlmonks can get you any answer you need beyond the docs...and you can submit patches for the docs, although I could never figure out how to do it. – 7stud Jul 30 '14 at 18:06