Method: REXML::Element#add_element
- Defined in:
- lib/rexml/element.rb
#add_element(element, attrs = nil) ⇒ Object
:call-seq:
add_element(name, attributes = nil) -> new_element
add_element(element, attributes = nil) -> element
Adds a child element, optionally setting attributes on the added element; returns the added element.
With string argument name, creates a new element with that name and adds the new element as a child:
e0 = REXML::Element.new('foo')
e0.add_element('bar')
e0[0] # => <bar/>
With argument name and hash argument attributes, sets attributes on the new element:
e0.add_element('baz', {'bat' => '0', 'bam' => '1'})
e0[1] # => <baz bat='0' bam='1'/>
With element argument element, adds that element as a child:
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
e0.add_element(e1)
e0[0] # => <bar/>
With argument element and hash argument attributes, sets attributes on the added element:
e0.add_element(e1, {'bat' => '0', 'bam' => '1'})
e0[1] # => <bar bat='0' bam='1'/>
725 726 727 728 729 730 731 732 |
# File 'lib/rexml/element.rb', line 725 def add_element element, attrs=nil raise "First argument must be either an element name, or an Element object" if element.nil? el = @elements.add(element) attrs.each do |key, value| el.attributes[key]=value end if attrs.kind_of? Hash el end |