string as nil or empty string?

A

aidy

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
 
R

Robert Klemme

aidy said:
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
from (irb):3:in `initialize'
from (irb):3
from :0
irb(main):004:0> String.new "foo"
=> "foo"

HTH

Kind regards

robert
 
F

Farrel Lifson

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

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
 
C

ChrisH

Robert said:
...
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
 
R

Robert Klemme

ChrisH said:
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

robert
 
F

Francis Cianfrocca

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

Eero Saynatkari

aidy said:
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 :)
 
J

Just Another Victim of the Ambient Morality

Francis Cianfrocca said:
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

It's funny that you would phrase the meaning of nil as you did, since
nil is, itself, an object. The reality of Ruby is that all variables
reference an object and there's no way to avoid this...
 
R

Rimantas Liubertas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top