class REXML::Elements
def inject( xpath=nil, initial=nil )
end # => 26
total += element.size
elements.inject('//book [@category="web"]', 0) do |total, element|
calls the block only with elements matching that xpath:
With both arguments +xpath+ and +initial+ are given,
end # => 44
total += element.size
elements.inject(nil, 0) do |total, element|
In this form the passed object can be used as an accumulator:
[3, 4]
[2, 3]
[1, 2]
[-1, 1]
Output:
end
element
p [elements.index(object), elements.index(element)]
elements.inject(nil, 'Initial') do |object, element|
In this example, the first object index is -1
- And so on.
- The third call passes the second block return value and the third element.
- The second call passes the first block return value and the second element.
- The first call passes the +initial+ and the first element.
calls the block once for each element.
and argument +initial+ also given,
With argument +xpath+ given as +nil+
[3, 4]
Output:
end
element
p [elements.index(object), elements.index(element)]
elements.inject('//book [@category="web"]') do |object, element|
elements matching that xpath:
With the single argument +xpath+, calls the block only with
[3, 4]
[2, 3]
[1, 2]
Output:
end
element
p [elements.index(object), elements.index(element)]
elements.inject do |object, element|
elements = d.root.elements
d = REXML::Document.new(xml_string)
which is then the object argument to the next call:
In this example, the block returns the passed element,
- And so on.
- The third call passes the second block return value and the fourth element.
- The second call passes the first block return value and the third element.
- The first call passes the first and second elements.
elements.size - 1 times.
With no argument, iterates over the elements, calling the block
Calls the block with elements; returns the last block return value.
inject(xpath = nil, initial = nil) -> object
:call-seq:
def inject( xpath=nil, initial=nil ) first = true XPath::each( @element, xpath ) {|e| if (e.kind_of? Element) if (first and initial == nil) initial = e first = false else initial = yield( initial, e ) if e.kind_of? Element end end } initial end