On Apr 13, 1:53 pm, Brian Candler <B.Cand... / pobox.com> wrote:
> On Fri, Apr 13, 2007 at 08:45:08PM +0900, chickenkiller wrote:
> > On Apr 13, 12:10 pm, Bontina Chen <abonc... / gmail.com> wrote:
> > > Lionel Orry wrote:
> > > > On Apr 13, 10:11 am, Bontina Chen <abonc... / gmail.com> wrote:
> > > >> <dc:creator>morwyn</dc:creator>
>
> > > >> but I got
> > > >> Posted viahttp://www.ruby-forum.com/.
> > > > replace innerTEXT by inner_html:
>
> > > > (doc/"item").each do |t|
> > > >    puts (t/"dc:subject").inner_html
> > > > end
>
> > > > regards
> > > > Lionel
>
> > > Thx for your response , but I still get
> > > <dc:subject>html internet tutorial web</dc:subject>
>
> > > --
> > > Posted viahttp://www.ruby-forum.com/.
>
> > In fact, inner_text works as well. But you should have a look at the
> > warnings from ruby! The inner_text or inner_html function is applied
> > to 'puts (t/"dc:subject")' return object, which is nil.
> > So a warning appears:
> > rdf.rb:6: undefined method `inner_html' for nil:NilClass
> > (NoMethodError)
>
> That's not a warning, that's an exception, and the program will terminate at
> that point. The OP didn't mention any errors.

Indeed I use the term 'warning' VERY abusively - I apologize for this.
This is an exception and nothing else.

>
> > but 'puts (t/"dc:subject")' is executed, and so '<dc:subject>html
> > internet tutorial web</dc:subject>' is displayed anyway. Therefore I
> > recommend using a few parentheses there:
>
> > puts((t/"dc:subject").inner_text)
>
> > and it should work well this time.
>
> > Next time, look at the warnings!!! ;)
>
> Good point, but it was OK the way he wrote it, with a space after puts.
>
> irb(main):003:0> p (1+3).to_s
> "4"
> => nil
> irb(main):004:0> p(1+3).to_s
> 4
> => ""
>
> In the first case, this is   p( (1+3).to_s )
>
> In the second case, this is  ( p(1+3) ).to_s   # i.e. nil.to_s

mmmh... interesting... It seems that the problem arises when in a
block:

# output text in comments...
require 'hpricot'

doc = Hpricot(File.open("rdf.xhtml"))

puts (doc/"item"/"dc:subject").inner_text
# html imported webpagedesign

(doc/"item").each do |t|
   puts((t/"dc:subject").inner_text)
end
# html imported webpagedesign

(doc/"item").each do |t|
   puts (t/"dc:subject").inner_text
end
# <dc:subject>html imported webpagedesign</dc:subject>
# rdf.rb:12: warning: don't put space before argument parentheses
# rdf.rb:12: undefined method `inner_text' for nil:NilClass
(NoMethodError)
# from rdf.rb:11:in `each'
# from rdf.rb:11

I am wondering where the difference is between the two last blocks.
Any ideas?

Lionel