Method: REXML::Element#add_text

Defined in:
lib/rexml/element.rb

#add_text(text) ⇒ Object

A helper method to add a Text child. Actual Text instances can be added with regular Parent methods, such as add() and <<()

text

if a String, a new Text instance is created and added to the parent. If Text, the object is added directly.

Returns

this Element

e = Element.new('a') #-> e.add_text 'foo' #-> foo e.add_text Text.new(' bar') #-> foo bar Note that at the end of this example, the branch has 3 nodes; the 'e' element and 2 Text node children.



523
524
525
526
527
528
529
530
531
532
533
# File 'lib/rexml/element.rb', line 523

def add_text( text )
  if text.kind_of? String
    if @children[-1].kind_of? Text
      @children[-1] << text
      return
    end
    text = Text.new( text, whitespace(), nil, raw() )
  end
  self << text unless text.nil?
  return self
end