mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
d71b5394ac
commit
b23eac6958
3 changed files with 48 additions and 0 deletions
6
NEWS
6
NEWS
|
@ -251,6 +251,12 @@ with all sufficient information, see the ChangeLog file or Redmine
|
|||
* Readline.quoting_detection_proc and Readline.quoting_detection_proc=
|
||||
[Feature #12659]
|
||||
|
||||
* REXML
|
||||
|
||||
* REXML::Element#[]: If String or Symbol is specified, attribute
|
||||
value is returned. Otherwise, Nth child is returned. This is
|
||||
backward compatible change.
|
||||
|
||||
* set
|
||||
|
||||
* New methods: Set#compare_by_identity and Set#compare_by_identity?.
|
||||
|
|
|
@ -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
|
||||
|
|
18
test/rexml/test_element.rb
Normal file
18
test/rexml/test_element.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# frozen_string_literal: false
|
||||
|
||||
require "test/unit/testcase"
|
||||
require "rexml/document"
|
||||
|
||||
module REXMLTests
|
||||
class ElementTester < Test::Unit::TestCase
|
||||
def test_array_reference_string
|
||||
doc = REXML::Document.new("<language name='Ruby'/>")
|
||||
assert_equal("Ruby", doc.root["name"])
|
||||
end
|
||||
|
||||
def test_array_reference_symbol
|
||||
doc = REXML::Document.new("<language name='Ruby'/>")
|
||||
assert_equal("Ruby", doc.root[:name])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue