[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: undefined method `string'

Gavin Kistner

11/13/2006 10:21:00 PM

From: Comfort Eagle
> Why does this work:
...
> puts soup.a
...
> but adding .string a la:
...
> puts soup.a.string
...
> results in:
> undefined method `string' for nil:NilClass (NoMethodError) ??

This means that the return value of "soup.a" is nil. Nil is an object in
Ruby, so:
puts nil
passes a nil object to the 'puts' method. The 'puts' method calls the
'to_s' method on the argument(s) passed to it. Nil has a to_s method
(that returns an empty string), so all is well.

However, nil does not have a 'string' method, which is the error you are
getting. Perhaps you are confusing Ruby with another language, and
actually meant:
puts soup.a.to_s
?

1 Answer

Comfort Eagle

11/13/2006 10:45:00 PM

0

Gavin Kistner wrote:
> From: Comfort Eagle
>> Why does this work:
> ...
>> puts soup.a
> ...
>> but adding .string a la:
> ...
>> puts soup.a.string
> ...
>> results in:
>> undefined method `string' for nil:NilClass (NoMethodError) ??
>
> This means that the return value of "soup.a" is nil. Nil is an object in
> Ruby, so:
> puts nil
> passes a nil object to the 'puts' method. The 'puts' method calls the
> 'to_s' method on the argument(s) passed to it. Nil has a to_s method
> (that returns an empty string), so all is well.
>
> However, nil does not have a 'string' method, which is the error you are
> getting. Perhaps you are confusing Ruby with another language, and
> actually meant:
> puts soup.a.to_s
> ?

Perhaps, but soup.a gives me actual results other than nil. ".a" is a
rubyful_soup method.

//Hoping I explained that correctly.


--
Posted via http://www.ruby-....