[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

string as nil or empty string?

aidy

9/11/2006 2:21:00 PM

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

8 Answers

Robert Klemme

9/11/2006 2:28:00 PM

0

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

HTH

Kind regards

robert

Farrel Lifson

9/11/2006 2:33:00 PM

0

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

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

Chris Hulan

9/11/2006 2:40:00 PM

0


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

Robert Klemme

9/11/2006 3:25:00 PM

0

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

robert

Francis Cianfrocca

9/11/2006 3:46:00 PM

0

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

e

9/11/2006 7:31:00 PM

0

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

Just Another Victim of the Ambient Morality

9/11/2006 7:52:00 PM

0


"Francis Cianfrocca" <garbagecat10@gmail.com> wrote in message
news:3a94cf510609110845n6e04e43cta69f81376a2307c4@mail.gmail.com...
> On 9/11/06, aidy <aidy.rutter@gmail.com> wrote:
>
> 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...




Rimantas Liubertas

9/11/2006 10:41:00 PM

0

<...>
> 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://rim...