From: yermej Date: 2007-11-18T01:20:02+09:00 Subject: Re: Weird behavior with hpricot and variable assignment On Nov 17, 8:06 am, ridcully wrote: > Hi, > > Could someone please help me to understand the difference of: > > html = Hpricot( "
" ) > (html/'/div').each do |item| > x = (item/'/div')[0].to_html > p x > end > > output: "
" > > and > > html = Hpricot( "
" ) > (html/'/div').each do |item| > p (item/'/div')[0].to_html > end > > gives "undefined method `[]' for nil:NilClass (NoMethodError)" in line > 3 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)