# String as nil or empty string?

**URL:** <https://rubytalk.org/t/string-as-nil-or-empty-string/30883>\
**Category:** ruby-talk\
**Created:** [11 September 2006 14:25 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883 "2006-09-11T14:25:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy2](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aidy2](https://rubytalk.org/u/aidy2)\
**Post date:** [11 September 2006 14:25 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/1 "2006-09-11T14:25:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [11 September 2006 14:33 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/2 "2006-09-11T14:33:25Z")

</div>

I prefer using nil that way I can do if statements like

if a\_string  
...  
end

instead of

if a\_string.empty?  
...  
end

The first example is more generic as not all objects respond to .empty?

Farrel

> **···**
>
> On 11/09/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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 September 2006 14:35 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/3 "2006-09-11T14:35:34Z")

</div>

aidy 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

It depends on the circumstances.

However, an additional note: it may be that you have a misconception about variables in Ruby. Although you named it "a\_string" this variable is typeless and it could hold a reference to \*any\* Ruby object. So a\_string "is" not a String as long as it doesn't point to one. And you cannot initialize a String with nil either:

irb(main):003:0\> String.new nil  
TypeError: can't convert nil into String  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):3:in `initialize'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):3  
irb(main):004:0\> String.new "foo"  
=\> "foo"

HTH

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> from :0

---

<div class="post-metadata">

**Author:** ![Francis\_Cianfrocca](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@Francis\_Cianfrocca](https://rubytalk.org/u/Francis_Cianfrocca)\
**Post date:** [11 September 2006 15:45 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/4 "2006-09-11T15:45:37Z")

</div>

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  
&nbsp;&nbsp;# 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).

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![E\_S](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@E\_S](https://rubytalk.org/u/E_S)\
**Post date:** [11 September 2006 19:31 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/5 "2006-09-11T19:31:12Z")

</div>

aidy 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

I would recommend, whenever possible, deferring initialisation  
of a String until you actually have said String 🙂

> **···**
>
> > aidy
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [11 September 2006 14:45 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/6 "2006-09-11T14:45:39Z")

</div>

Robert Klemme wrote:

> aidy wrote:  
> \> Hi,  
> \>  
> \> With everything in Ruby being an object, should a string be initialised  
> \> as an empty string  
> \>  
> ...  
> However, an additional note: it may be that you have a misconception  
> about variables in Ruby. Although you named it "a\_string" this variable  
> is typeless and it could hold a reference to \*any\* Ruby object. So  
> a\_string "is" not a String as long as it doesn't point to one. And you  
> cannot initialize a String with nil either:

...

+1 Robert

Every thing is an object, but varaibles are references to objects.  
If you don't specify an object for a variable to refer to than nil  
seems  
the best default, yes?

Cheers

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [11 September 2006 21:19 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/7 "2006-09-11T21:19:22Z")

</div>

> \<split\>
> 
> nil signifies the absence of an object, so you can always set a

strange sentence you have written, I am quite surprised,  
nil is an object and can thus not signify the absence of an object I am sure  
you agree  
and yet of course what you say is very much what we all mean most of the  
time when we use nil  
Look at the following code I use often

x = nil # need to define x outside of blocks I will use it in  
# note that I do not use nil to identify falseness or absence I just use  
# it because the language foreces me to assign a value to x and nil comes  
# into my mind, but somehow I doubt that it is such a good idea  
{ ... using x ...}

Why the heck do I not write  
x = Object.new  
{ ... using x ...}

or just of course  
x = 42  
{ ... using x }

It is very very easy to slip from definitions to conventions or rather the  
other way round.

\<valid points snipped\>...

> 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).

Again interesting that you kind of have this unorthogonal view, as you could  
have written,

are instanecs of the classes NilClass and FalseClass

I wonder for a long time about the concept of having a value indicating the  
absence of a value.  
I think the conecpt is flawed -useful- but flawed.

Any thaughts?

Cheers  
Robert

P.S.  
(1) maybe it would be nice to have one singlteon object None or Void or  
Omega that has the special property of not having any method and to raise an  
UndefinedError whenever read.  
Maybe I will put this into my toolbox.  
class VoidClass  
&nbsp;&nbsp;&nbsp;... undefine all instance methods  
&nbsp;&nbsp;def method\_missing(\*a,&b); raise UndefinedError, "You tried to access an  
undefined object"  
&nbsp;&nbsp;...

R.

> **···**
>
> On 9/11/06, Francis Cianfrocca \<garbagecat10@gmail.com\> wrote:
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 September 2006 15:30 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/8 "2006-09-11T15:30:32Z")

</div>

ChrisH wrote:

> Every thing is an object, but varaibles are references to objects.  
> If you don't specify an object for a variable to refer to than nil  
> seems the best default, yes?

Often yes, but this may vary according to circumstances. Since I haven't seen more detail that's about as specific I will get here. 🙂

Kind regards

&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Rimantas\_Liubertas](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Rimantas\_Liubertas](https://rubytalk.org/u/Rimantas_Liubertas)\
**Post date:** [11 September 2006 22:41 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/9 "2006-09-11T22:41:16Z")

</div>

\<...\>

> I wonder for a long time about the concept of having a value indicating the  
> absence of a value.  
> I think the conecpt is flawed -useful- but flawed.

\<...\>

What alternatives do we have? For me it is perfectly natural.  
In natural languages we have words which denote nothingness,  
why can't we have objects for the same purpose in programming languages?

Regards,  
Rimantas

> **···**
>
> --  
> [http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [12 September 2006 08:19 UTC](https://rubytalk.org/t/string-as-nil-or-empty-string/30883/10 "2006-09-12T08:19:51Z")

</div>

> \<...\>  
> \> I wonder for a long time about the concept of having a value indicating  
> the  
> \> absence of a value.  
> \> I think the conecpt is flawed -useful- but flawed.  
> \<...\>
> 
> What alternatives do we have?

You read me very well, we might not have any, still I think it is flawed  
like as our perception we can put into the modelling process a  
programming language is, might be too limited yet.

For me it is perfectly natural.

> In natural languages we have words which denote nothingness,

Yep, but see what is behind it,  
shall we use nil for  
nothingless or defaults or undefined values or place holders or nil, please  
do not forget that nil comes from a very specific meaning, but maybe we just  
\*shall\* forget that.

why can't we have objects for the same purpose in programming languages?

objects by all means, we need objects in Ruby there is nothing else, but  
maybe nil  
is not the object semantically behaving correctly to model e.g.  
nothinglessness  
c.f. my None implementation above  
I feel that if nil models the absence of a value (which it does not quite  
accurately) maybe it should not model other things and we should be more  
creative ourselfs when using nil.

Maybe we should also have the possibility (as in Python, sorry I said the  
word) to define  
objects that evaluate to false

like e.g.

x evaluates to false in boolean context if one of the three conditions is  
met  
(1) x.respond\_to?(:I\_have\_no\_idea\_maybe\_true\_value?) && x.true\_value? meets  
conditions (1???) (2) or (3)  
(2) x==false  
(3) x.nil?

anyway please note that these are ideas, feelings not concrete ideas and for  
sure no rules 😉

Regards,

> **···**
>
> On 9/12/06, Rimantas Liubertas \<rimantas@gmail.com\> wrote:
> 
> > Rimantas  
> > --  
> > [http://rimantas.com/](http://rimantas.com/)
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein
