# A few confusing Hpricot outputs. Anyone had similar experience?

**URL:** <https://www.ruby-forum.com/t/a-few-confusing-hpricot-outputs-anyone-had-similar-experience/164830>\
**Category:** Ruby\
**Created:** [April 6, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/a-few-confusing-hpricot-outputs-anyone-had-similar-experience/164830 "2009-04-06T13:12:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wang\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f08c70/32.png) [@Wang\_J](https://www.ruby-forum.com/u/Wang_J)\
**Post date:** [April 6, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/a-few-confusing-hpricot-outputs-anyone-had-similar-experience/164830/1 "2009-04-06T13:12:48Z")

</div>

## I wanted to work on something like the following example string

require ‘hpricot’  
string = ‘[posted on  
April  
2009](https://www.ruby-forum.com/123/456 "2009-04-06")’  
h = Hpricot(string)  
t = “2009-04-06”

## Here it goes: confusion No.1

h.at(‘a[@title\*=“2009-04-06”]’)  
##=\> returns the 2nd anchor element, as expected.  
h.at(‘a[@title\*=Time.now.strftime(“%Y-%m-%d”)]’)  
##=\> _1st anchor element. Why is that??_  
h.at(“a[@title\*=#{t}]”)  
##=\> 2nd anchor. works fine  
h.at(‘a[@title\*=“#{t}”]’)  
##=\> _nil. Because of the single quote?_

## And here comes another confusion:

year = “2009”  
h.at(“a[@title\*=#{t}][text()_=‘2009’]")  
##=\> 2nd anchor, as expected.  
h.at("a[@title_=#{t}][text()\*=#{year}]”)  
##=\> _nil. Why is that? Hpricot can’t handle #{} more than once?_

## Hope you can fill me in on this one. Thanks!!

##Jay

---

<div class="post-metadata">

**Author:** ![Wang\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f08c70/32.png) [@Wang\_J](https://www.ruby-forum.com/u/Wang_J)\
**Post date:** [April 6, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/a-few-confusing-hpricot-outputs-anyone-had-similar-experience/164830/2 "2009-04-06T16:56:59Z")

</div>

On Mon, Apr 6, 2009 at 4:11 AM, Wang J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> h.at(‘a[@title\*=“2009-04-06”]’)  
> ##=\> returns the 2nd anchor element, as expected.  
> h.at(‘a[@title\*=Time.now.strftime(“%Y-%m-%d”)]’)  
> ##=\> _1st anchor element. Why is that??_

I’m not sure why it is returning {emptyelem }, but I can tell you  
why its not returning the element you expect: because you didn’t use  
string interpolation so that the call to Time.now.strftime(…) would  
be evaluated and inserted into the string. This selects the expected  
element:

 

h.at(“a[@title\*=#{Time.now.strftime(‘%Y-%m-%d’)}]”)

 

> h.at(“a[@title\*=#{t}]”)  
> ##=\> 2nd anchor. works fine  
> h.at(‘a[@title\*=“#{t}”]’)  
> ##=\> _nil. Because of the single quote?_

 

Exactly, that’s just ruby single- versus double-quote string behavior.  
With the same setup as you used:

 

irb(main):037:0\> “#{t}”  
=\> “2009-04-06”  
irb(main):038:0\> ‘#{t}’  
=\> “#{t}”

 

> ## And here comes another confusion:
> 
> year = “2009”  
> h.at(“a[@title\*=#{t}][text()_=‘2009’]")  
> ##=\> 2nd anchor, as expected.  
> h.at("a[@title_=#{t}][text()\*=#{year}]”)  
> ##=\> _nil. Why is that? Hpricot can’t handle #{} more than once?_

 

Do you mean for these to pass different strings to h.at()? Look at the  
strings you are using.

 

irb(main):048:0\> puts ["a[@title\*=#{t}][text()_=‘2009’]",  
irb(main):049:1_ "a[@title\*=#{t}][text()_=#{year}]" ]  
a[@title_=2009-04-06][text()_=‘2009’]  
a[@title_=2009-04-06][text()\*=2009]

 

So, you are just getting unreliable results when you aren’t using  
quotes around the values you are searching for. This version works,  
where the second one above did not:

 

h.at(“a[@title\*=‘#{t}’][text()\*=‘#{year}’]”)

 

Note that I’ve put quotes on both values, though at least in this  
example the title appears to work without them.

---

<div class="post-metadata">

**Author:** ![Wang\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f08c70/32.png) [@Wang\_J](https://www.ruby-forum.com/u/Wang_J)\
**Post date:** [April 7, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/a-few-confusing-hpricot-outputs-anyone-had-similar-experience/164830/3 "2009-04-07T12:07:30Z")

</div>

Great notes. Thanks a lot!

So the take home message is like always use " on the very outside, and  
use  
(literally) ‘#{expression}’ to ensure consistency.

It’s kinda counter-intuitive at first look, as normally the #{} won’t  
work  
when placed in between single quotes. But it works in this one. 🙂

2009/4/6 Christopher D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
