http://www.ruby-doc.org/stdlib-1.9.3/libdoc/rexml/rdoc/REXML/Elements.html#method-i-each
includes the following example:
doc = Document.new '<a><b/><c/><d/>sean<b/><c/><d/></a>'
doc.root.each {|e|p e} #-> Yields b, c, d, b, c, d elements
doc.root.each('b') {|e|p e} #-> Yields b, b elements
However when I run it I get the results below.
Am I doing something wrong? If not, is the documentation or the code
correct? Should I be reporting this somewhere?
(My underlying goal is to find an iterator that yields one element at a
time from an XPath search, rather than doing the whole search to return
a complete array of elements.)
Running the example:
Sleek:iTunes_Library_Update Wes$ rvm 1.9.3
Sleek:iTunes_Library_Update Wes$ ruby -v
ruby 1.9.3p327 (2012-11-10) [x86_64-darwin12.2.0]
Sleek:iTunes_Library_Update Wes$ ruby rexml_question.rb
<b/>
<c/>
<d/>
"sean"
<b/>
<c/>
<d/>
/Users/Wes/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/rexml/parent.rb:40:in
`each': wrong number of arguments (1 for 0) (ArgumentError)
from rexml_question.rb:10:in `<main>'
Sleek:iTunes_Library_Update Wes$ cat < rexml_question.rb
#! /usr/bin/env ruby
require "rexml/document"
include REXML
STDOUT.sync = true
doc = Document.new '<a><b/><c/><d/>sean<b/><c/><d/></a>'
doc.root.each {|e|p e} #-> Yields b, c, d, b, c, d elements
doc.root.each('b') {|e|p e} #-> Yields b, b elements
doc.root.each('child::node()') {|e|p e}
#-> Yields <b/>, <c/>, <d/>, <b/>, <c/>, <d/>
XPath.each(doc.root, 'child::node()', &block)
#-> Yields <b/>, <c/>, <d/>, sean, <b/>, <c/>, <d/>
Sleek:iTunes_Library_Update Wes$
···
--
Posted via http://www.ruby-forum.com/.