If I have an instance of REXML::Element, is there a convenient way to get it's element name? I would have expected a get_name method for Element or something like that, but according to the stdlib documentation at http://www.ruby-doc.org/stdlib/ there is no such thing.
I found two ways that I do not like very much:
1. get the xpath to the element via Element.xpath and then extract the actual name:
path = el.xpath
name = path.sub(/^\/(.+\/)*(\w*)(\[\d*\])?/ ,'\2')
I don't like this method since it feels wasteful.
2. Use instance_eval to get the value of Element::expanded_name:
irb(main):013:0> el.instance_eval('@expanded_name')
=> "import"
I don't like that either (I know I'm picky) since this means to use part of the rexml implementation that is not part of the publicly documented interface.
Is there an other way that I would like?
Thanks in advance for all advise and insights.
regards
Jerry
It's in the included REXML::Namespace module:
http://ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Namespace.html
element.name
Cheers,
/Nick
···
On 3/26/06, Gerald Preissler <Gerald.Preissler@gmx.de> wrote:
If I have an instance of REXML::Element, is there a convenient way to
get it's element name?
ruby style avoids extraneous get prefixes to the front of method names. It's just name, not get_name
···
On Mar 26, 2006, at 9:23 PM, Gerald Preissler wrote:
If I have an instance of REXML::Element, is there a convenient way to get it's element name? I would have expected a get_name method for Element or something like that, but according to the stdlib documentation at http://www.ruby-doc.org/stdlib/ there is no such thing.
Logan Capaldo wrote:
ruby style avoids extraneous get prefixes to the front of method names. It's just name, not get_name
Logan,
thanks to you and Nick for your replies.
Shouldn't that show up in the the docsas an attribute of Element?
regards
Jerry
"Nick Sieger" <nicksieger@gmail.com> writes:
If I have an instance of REXML::Element, is there a convenient way to
get it's element name?
It's in the included REXML::Namespace module:
http://ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Namespace.html
element.name
How can one figure that out in pure Ruby? It's rather easy, once you
know the trick.
irb(main):002:0> require 'rexml/document'
=> true
irb(main):003:0> REXML::Element.instance_method(:name)
=> #<UnboundMethod: REXML::Element(REXML::Namespace)#name>
^^^^^^^^^^^^^^^^
irb(main):004:0> REXML::Element.instance_method(:to_s)
=> #<UnboundMethod: REXML::Element(REXML::Node)#to_s>
^^^^^^^^^^^
So, if you *know* you can call a method foo on objects of class Bar,
but just can't find it anywere, inspect Bar.instance_method :foo and
you'll see the defining module.
···
On 3/26/06, Gerald Preissler <Gerald.Preissler@gmx.de> wrote:
Cheers,
/Nick
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
It's a downside of rdoc. Up at the top[1] you see that the Namespace module is mixed in, but it's methods do not show up. ri actually shows the mixed in methods -- I don't know why the HTML docs can't.
[1] http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Element.html
-- Daniel
···
On Mar 27, 2006, at 8:03 AM, Gerald Preissler wrote:
Shouldn't that show up in the the docsas an attribute of Element?