Created: 2012-03-20 20:50
Updated: 2017-08-10 16:36
License: other → Ruby →
REXML::Element.new_with_options methods to build elements


This adds convenience methods to REXML::Element so we can create an Element easily.


e = Element.new_with_options(:name => "foo", :text => "bar")                                                                                                                                                                                                           
#=> element with name "foo" and text "bar"  

There are options for creating an Element with children, attributes, text, and additional methods that can create a table data cell and table row.

For docs go to

Want to help? We're happy to get pull requests.



Run this command in your shell or terminal:

gem install sixarm_ruby_rexml_element_new_with_options

Or add this to your Gemfile:

gem 'sixarm_ruby_rexml_element_new_with_options'


To require the gem in your code:

require 'sixarm_ruby_rexml_element_new_with_options'

Example of children

child_1 =
child_2 =
child_3 =
e = Element.new_with_options(:children => [child_1, child_2, child_3])    
#=> element that has child elements

Example of table methods

e = Element.new_with_options_as_td(:text => "bar")                                                                                                                                                                                                                     
#=> element with name "td" and text "bar"  

e = Element.new_with_options_as_tr(:text => "bar")                                                                                                                                                                                                                     
#=> element with name "tr" and text "bar"  

e = Element.new_with_options_as_tr(:cells => ["a", "b", "c"])                                                                                                                                                                                                               
#=> element with name=="tr" and children elements with name "td" and text "a", "b", "c" 
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more