# Hpricot question

**URL:** <https://www.ruby-forum.com/t/hpricot-question/142872>\
**Category:** Ruby\
**Created:** [July 12, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/hpricot-question/142872 "2008-07-12T14:41:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rainer](https://www.ruby-forum.com/u/rainer)\
**Post date:** [July 12, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/hpricot-question/142872/1 "2008-07-12T14:41:20Z")

</div>

Hello,

I’m a humble beginner with Hpricot. I want to write a method that  
performs certain actions for every HTML tag of a given HTML fragment.  
In the documentation, I have only found search mechanisms that to for  
a specific tag, attribute or id. What I need, however is a method that  
tells me the first HTML tag of a fragment, whatever it is. For  
instance:

```
a_fragment = "<div id="my_first_div">first div<p>a paragraph</

```

p\>a span"

Are there methods like “current\_tag()” or “current\_id()” in Hpricot,  
which act like this:

```
s = current_tag(a_fragment)
puts s
=>
"div"

```

or  
s2 = current\_id(a\_fragment)  
puts s2  
=\>  
“my\_first\_id”

Any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![rainer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rainer](https://www.ruby-forum.com/u/rainer)\
**Post date:** [July 12, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/hpricot-question/142872/2 "2008-07-12T17:03:15Z")

</div>

Rainer wrote:

> What I need […] is a method that  
> tells me the first HTML tag of a fragment, whatever it is.

You may want to use Element#name:

$ irb -r hpricot

> > d = Hpricot(‘
> > 
> > first div
> > 
> > a paragraph
> > 
> > a span
> > ’)  
> > =\> #\<Hpricot::Doc {elem 
> > “first div” {elem 
> > 
> > “a  
> > paragraph\</\np\>” {elem “a span” }}
> > 
> > }\>
> 
> > d.search(’\*’).first.name  
> > =\> “div”

Regards, Ã–mer [yet another Ruby beginner].

---

<div class="post-metadata">

**Author:** ![rainer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rainer](https://www.ruby-forum.com/u/rainer)\
**Post date:** [July 13, 2008, 6:49am UTC](https://www.ruby-forum.com/t/hpricot-question/142872/3 "2008-07-13T06:49:59Z")

</div>

> What I need, however is a method that tells me the first HTML tag of a fragment, whatever it is

Try this:

a\_fragment = ‘

first div

a paragraph

a  
span
’

doc=Hpricot(a\_fragment)

tag=doc.search("[1]").first.name

=\> “div”

---

<div class="post-metadata">

**Author:** ![rainer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rainer](https://www.ruby-forum.com/u/rainer)\
**Post date:** [July 13, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/hpricot-question/142872/4 "2008-07-13T23:29:19Z")

</div>

Thank you Ömer,  
Thank you Dan,

that did the trick. However, I couldn’t find anything about  
Hpricot::Elem in the official rdoc:

[http://code.whytheluckystiff.net/doc/hpricot/](http://code.whytheluckystiff.net/doc/hpricot/)

After you told me about it, I could find the helpful “name” method  
with

puts doc.search(“[1]”).first.methods.sort

Ömer and Dan, how did you find out about the methods of Hpricot::Elem  
and “name”, in particular?

Kind regards,

Rainer
