Francis Cianfrocca
9/11/2006 3:46:00 PM
On 9/11/06, aidy <aidy.rutter@gmail.com> wrote:
> Hi,
>
> With everything in Ruby being an object, should a string be initialised
> as an empty string
>
> e.g.
>
> a_string = ""
>
> or nil
>
> e.g
>
> a_string = nil
>
> cheers
>
> aidy
>
>
>
nil signifies the absence of an object, so you can always set a
variable to nil, regardless of whether the variable was previously
bound to an object of type String, some other type, or indeed nil. I
wonder if what you really want is to impute a logical-false value to
an empty string, to enable expressions like
s = String.new
...
unless s
# here the string is known to be not empty
end
This would violate a fairly deep expectation that the only values in
Ruby which evaluate to logical-false are nil and instances of class
FalseClass (of which there is only one, of course).