# Weird behavior with hpricot and variable assignment

**URL:** <https://rubytalk.org/t/weird-behavior-with-hpricot-and-variable-assignment/42284>\
**Category:** ruby-talk\
**Created:** [17 November 2007 14:10 UTC](https://rubytalk.org/t/weird-behavior-with-hpricot-and-variable-assignment/42284 "2007-11-17T14:10:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ridcully](https://avatars.discourse-cdn.com/v4/letter/r/5f9b8f/32.png) [@ridcully](https://rubytalk.org/u/ridcully)\
**Post date:** [17 November 2007 14:10 UTC](https://rubytalk.org/t/weird-behavior-with-hpricot-and-variable-assignment/42284/1 "2007-11-17T14:10:11Z")

</div>

Hi,

Could someone please help me to understand the difference of:

html = Hpricot( "\<div\>\<div\>\</div\>\</div\>" )  
(html/'/div').each do |item|  
&nbsp;&nbsp;x = (item/'/div')[0].to\_html  
&nbsp;&nbsp;p x  
end

output: "\<div\>\</div\>"

and

html = Hpricot( "\<div\>\<div\>\</div\>\</div\>" )  
(html/'/div').each do |item|  
&nbsp;&nbsp;p (item/'/div')[0].to\_html  
end

gives "undefined method `[]' for nil:NilClass (NoMethodError)" in line  
3

Shouldn't the code snippets semantically be the same?

Thanks,  
Andreas

---

<div class="post-metadata">

**Author:** ![yermej](https://avatars.discourse-cdn.com/v4/letter/y/b2d939/32.png) [@yermej](https://rubytalk.org/u/yermej)\
**Post date:** [17 November 2007 16:20 UTC](https://rubytalk.org/t/weird-behavior-with-hpricot-and-variable-assignment/42284/2 "2007-11-17T16:20:02Z")

</div>

Try your second example in irb and you'll get a warning (or I do,  
anyway):

(irb):5: warning: don't put space before argument parentheses

This means you're essentially doing this:

(p(item/'/div'))[0].to\_html

and since p returns nil, you're calling on nil. Try this instead:

p((item/'/div')[0].to\_html)

> **···**
>
> On Nov 17, 8:06 am, ridcully \<goo...@ridcully.net\> wrote:
> 
> > Hi,
> > 
> > Could someone please help me to understand the difference of:
> > 
> > html = Hpricot( "\<div\>\<div\>\</div\>\</div\>" )  
> > (html/'/div').each do |item|  
> > &nbsp;&nbsp;x = (item/'/div')[0].to\_html  
> > &nbsp;&nbsp;p x  
> > end
> > 
> > output: "\<div\>\</div\>"
> > 
> > and
> > 
> > html = Hpricot( "\<div\>\<div\>\</div\>\</div\>" )  
> > (html/'/div').each do |item|  
> > &nbsp;&nbsp;p (item/'/div')[0].to\_html  
> > end
> > 
> > gives "undefined method `' for nil:NilClass (NoMethodError)" in line  
> > 3

---

<div class="post-metadata">

**Author:** ![ridcully](https://avatars.discourse-cdn.com/v4/letter/r/5f9b8f/32.png) [@ridcully](https://rubytalk.org/u/ridcully)\
**Post date:** [17 November 2007 22:35 UTC](https://rubytalk.org/t/weird-behavior-with-hpricot-and-variable-assignment/42284/3 "2007-11-17T22:35:05Z")

</div>

You're right, it does work if I add parentheses.  
Still I don't fully understand why this is happening. The operator  
obviously has a high presendence, because this of course does work:

p [1, 2, 3][0].to\_s

Also this does work fine:

p (html/'/div')[0].to\_html

I don't really see the difference to my second example. What am I  
missing here?

Andreas

> **···**
>
> On Nov 17, 5:16 pm, yermej \<yer...@gmail.com\> wrote:
> 
> > Try your second example in irb and you'll get a warning (or I do,  
> > anyway):
> > 
> > (irb):5: warning: don't put space before argument parentheses
> > 
> > This means you're essentially doing this:
> > 
> > (p(item/'/div'))[0].to\_html
> > 
> > and since p returns nil, you're calling on nil. Try this instead:
> > 
> > p((item/'/div')[0].to\_html)
