Method: REXML::Element#add_attribute
- Defined in:
- lib/rexml/element.rb
#add_attribute(key, value = nil) ⇒ Object
:call-seq:
add_attribute(name, value) -> value
add_attribute(attribute) -> attribute
Adds an attribute to this element, overwriting any existing attribute by the same name.
With string argument name and object value are given, adds the attribute created with that name and value:
e = REXML::Element.new
e.add_attribute('attr', 'value') # => "value"
e['attr'] # => "value"
e.add_attribute('attr', 'VALUE') # => "VALUE"
e['attr'] # => "VALUE"
With only attribute object attribute given, adds the given attribute:
a = REXML::Attribute.new('attr', 'value')
e.add_attribute(a) # => attr='value'
e['attr'] # => "value"
a = REXML::Attribute.new('attr', 'VALUE')
e.add_attribute(a) # => attr='VALUE'
e['attr'] # => "VALUE"
1336 1337 1338 1339 1340 1341 1342 |
# File 'lib/rexml/element.rb', line 1336 def add_attribute( key, value=nil ) if key.kind_of? Attribute @attributes << key else @attributes[key] = value end end |