Undefined method `string'

Why does this work:

require ‘rubygems’
require ‘rubyful_soup’

sourceFile = ‘lists.php’
desFile = ‘parsedtext.txt’

open(sourceFile).each { |x|
soup = BeautifulSoup.new(x)
puts soup.a
}

but adding .string a la:

require ‘rubygems’
require ‘rubyful_soup’

sourceFile = ‘lists.php’
desFile = ‘parsedtext.txt’

open(sourceFile).each { |x|
soup = BeautifulSoup.new(x)
puts soup.a.string
}

results in:
undefined method `string’ for nil:NilClass (NoMethodError) ??

Thanks in advance.

Comfort E. wrote:

soup = BeautifulSoup.new(x)

open(sourceFile).each { |x|
soup = BeautifulSoup.new(x)
puts soup.a.string
}

results in:
undefined method `string’ for nil:NilClass (NoMethodError) ??

Because soup.a is nil?
As the interpreter obediently informs you of.

“puts soup.a” works because to_s is defined on nil to return the empty
string.

David V.