Forum: Ruby rexml - how to get element name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
48b210717c45d69899f00d185f022a7d?d=identicon&s=25 Gerald Preissler (Guest)
on 2006-03-27 04:24
(Received via mailing list)
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
526d60de6472502bb570a9df2842b33b?d=identicon&s=25 Nick Sieger (Guest)
on 2006-03-27 04:29
(Received via mailing list)
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?


It's in the included REXML::Namespace module:

http://ruby-doc.org/stdlib/libdoc/rexml/rdoc/class...

element.name

Cheers,
/Nick
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-27 04:32
(Received via mailing list)
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.

ruby style  avoids extraneous get prefixes to the front of method
names. It's just name, not get_name
48b210717c45d69899f00d185f022a7d?d=identicon&s=25 Gerald Preissler (Guest)
on 2006-03-27 08:04
(Received via mailing list)
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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-27 15:22
(Received via mailing list)
On Mar 27, 2006, at 8:03 AM, Gerald Preissler wrote:

> Shouldn't that show up in the the docsas an attribute of Element?

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/c...
Element.html

-- Daniel
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-03-27 22:18
(Received via mailing list)
"Nick Sieger" <nicksieger@gmail.com> writes:

> 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.
This topic is locked and can not be replied to.