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.
Gerald P. (Guest)
on 2006-03-27 06: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
Nick S. (Guest)
on 2006-03-27 06:29
(Received via mailing list)
On 3/26/06, Gerald P. <removed_email_address@domain.invalid> 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
Logan C. (Guest)
on 2006-03-27 06:32
(Received via mailing list)
On Mar 26, 2006, at 9:23 PM, Gerald P. 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
Gerald P. (Guest)
on 2006-03-27 10:04
(Received via mailing list)
Logan C. 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
Daniel H. (Guest)
on 2006-03-27 17:22
(Received via mailing list)
On Mar 27, 2006, at 8:03 AM, Gerald P. 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
Christian N. (Guest)
on 2006-03-28 00:18
(Received via mailing list)
"Nick S." <removed_email_address@domain.invalid> 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.