[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rexml - how to get element name

Gerald Preissler

3/26/2006 7:32:00 PM

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.o... 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
5 Answers

Nick Sieger

3/27/2006 2:29:00 AM

0

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/classes/REXML/Name...

element.name

Cheers,
/Nick

Logan Capaldo

3/27/2006 2:33:00 AM

0


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.o... 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 Preissler

3/27/2006 5:58:00 AM

0

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

Daniel Harple

3/27/2006 1:23:00 PM

0

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

-- Daniel



Christian Neukirchen

3/27/2006 8:19:00 PM

0

"Nick Sieger" <nicksieger@gmail.com> writes:

> 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/classes/REXML/Name...
>
> 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.

> Cheers,
> /Nick
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...