aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rexml
diff options
context:
space:
mode:
authorkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-06 13:57:56 +0000
committerkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-06 13:57:56 +0000
commit1ce91d0c7c4acbe1f000d038f96b51c9f0d26fa6 (patch)
tree3d8271eca935ac631a90ff961f9131021e5da3f4 /lib/rexml
parentd23305c71e7481dba5bf61715bc626dc2c35fee1 (diff)
downloadruby-1ce91d0c7c4acbe1f000d038f96b51c9f0d26fa6.tar.gz
rexml: REXML::Element#[] accepts String or Symbol as attribute name
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57003 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rexml')
-rw-r--r--lib/rexml/element.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/rexml/element.rb b/lib/rexml/element.rb
index f725d5a2be..a9811bcba3 100644
--- a/lib/rexml/element.rb
+++ b/lib/rexml/element.rb
@@ -551,6 +551,30 @@ module REXML
# Attributes #
#################################################
+ # Fetches an attribute value or a child.
+ #
+ # If String or Symbol is specified, it's treated as attribute
+ # name. Attribute value as String or +nil+ is returned. This case
+ # is shortcut of +attributes[name]+.
+ #
+ # If Integer is specified, it's treated as the index of
+ # child. It returns Nth child.
+ #
+ # doc = REXML::Document.new("<a attr='1'><b/><c/></a>")
+ # doc.root["attr"] # => "1"
+ # doc.root.attributes["attr"] # => "1"
+ # doc.root[1] # => <c/>
+ def [](name_or_index)
+ case name_or_index
+ when String
+ attributes[name_or_index]
+ when Symbol
+ attributes[name_or_index.to_s]
+ else
+ super
+ end
+ end
+
def attribute( name, namespace=nil )
prefix = nil
if namespaces.respond_to? :key