[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Symbol vs String

Sebestyén Gábor

3/16/2005 8:37:00 PM

Hi,

Just a dumb question: what is the real difference between { :aKey =>
"aValue" } and { "aKey" => "aValue" } ? I know the first key is a
symbol the latter is a string. I like string keys why should I use
symbols? Why symbols worth to use as keys?
Thanks,

Gábor




19 Answers

Malte Milatz

3/16/2005 8:53:00 PM

0

Sebestyén Gábor:
> I like string keys why should I use symbols?

Because symbols
- are faster and
- save you one byte in your rb file.

Malte

Nikolai Weibull

3/16/2005 9:01:00 PM

0

* Sebestyén Gábor (Mar 16, 2005 21:40):
> Just a dumb question: what is the real difference between { :aKey =>
> "aValue" } and { "aKey" => "aValue" } ? I know the first key is a
> symbol the latter is a string. I like string keys why should I use
> symbols? Why symbols worth to use as keys? Thanks,

Always use symbols for situations like these. The reason is that a
symbol is immutable and also that no new string needs to be created for
it if used more than once. Also, using strings as symbols and then
having the string altered will force a rehash of the table. It's all
about memory savings and execution speed,
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Eric Hodel

3/16/2005 9:03:00 PM

0

On 16 Mar 2005, at 12:37, Sebestyén Gábor wrote:

> Hi,
>
> Just a dumb question: what is the real difference between { :aKey =>
> "aValue" } and { "aKey" => "aValue" } ? I know the first key is a
> symbol the latter is a string. I like string keys why should I use
> symbols? Why symbols worth to use as keys?

Symbols take up less memory space (only allocated once for the same
Symbol) and have a faster #hash function (#object_id, not computed).

'x' == 'x' # => true
'x'.object_id == 'x'.object_id # => false

:x == :x # => true
:x.object_id == :x.object_id # => true

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Eric Hodel

3/16/2005 9:12:00 PM

0

On 16 Mar 2005, at 13:00, Nikolai Weibull wrote:

> Also, using strings as symbols and then having the string altered will
> force a rehash of the table.

You mean this?

key = 'foo'

hash = {}

hash[key] = 5

key.gsub! /foo/, 'bar'

In this case, hash.rehash does not need to be called because Ruby
copies String hash keys:

hash.keys.first.object_id == key.object_id # => false

Also, String keys are frozen, so you can't modify them:

hash.keys.first.gsub! /foo/, 'bar' # => raises TypeError

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Nikolai Weibull

3/16/2005 9:27:00 PM

0

* Eric Hodel (Mar 16, 2005 22:20):
> > Also, using strings as symbols and then having the string altered
> > will force a rehash of the table.

[basically saying that this isn't so]

OK, so this strengthens the argument for using symbols even further, as
keys will be copied. Thanks for pointing this out,
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Robert Klemme

3/16/2005 9:31:00 PM

0


"Nikolai Weibull" <mailing-lists.ruby-talk@rawuncut.elitemail.org> schrieb
im Newsbeitrag news:20050316210032.GE5638@puritan.pcp.ath.cx...
>* Sebestyén Gábor (Mar 16, 2005 21:40):
>> Just a dumb question: what is the real difference between { :aKey =>
>> "aValue" } and { "aKey" => "aValue" } ? I know the first key is a
>> symbol the latter is a string. I like string keys why should I use
>> symbols? Why symbols worth to use as keys? Thanks,
>
> Always use symbols for situations like these. The reason is that a
> symbol is immutable and also that no new string needs to be created for
> it if used more than once. Also, using strings as symbols and then
> having the string altered will force a rehash of the table. It's all
> about memory savings and execution speed,

I rather make the distinction on the semantic level: for example, if you
write an initializer for a class that accepts a hash to init any number of
instance fields I'd prefer to use symbols here. Also, if there is only a
certain fixed set of values allowed. I use strings if they are read from
some source and I don't know beforehand, what they might be.

Incidentally it's typical for the key like things to occur rather often,
which fits nicely with the memory and speed savings incurred by symbols.

Kind regards

robert

Peter C. Verhage

3/16/2005 10:32:00 PM

0

But why do Strings not behave like Symbols? I mean, why aren't all
Strings immutable? Is this because Symbols will never get garbage
collected (to make sure they can be used over and over again) and normal
Strings will? Which might mean that in some cases (lots of text
processing) immutable Strings would fill up memory?

Regards,

Peter

Nikolai Weibull

3/16/2005 11:04:00 PM

0

* Peter C. Verhage (Mar 16, 2005 23:40):
> But why do Strings not behave like Symbols? I mean, why aren't all
> Strings immutable? Is this because Symbols will never get garbage
> collected (to make sure they can be used over and over again) and
> normal Strings will? Which might mean that in some cases (lots of text
> processing) immutable Strings would fill up memory?

Oh, no...not immutable vs. mutable strings again...

Well, if strings were immutable, then that would mean that strings could
share contents, and thus immutable strings wouldn't fill up memory. I
have suggested on the ruby-core list that Ruby should provide a second
data structure that acts like a string, namely the _rope_, and that it
be implemented in a way that allows for it to be used for tasks where
immutable "strings" are desired.

A rope is basically a string represented by a tree. Leafs of the tree
point to the subsequences of the whole string. These subsequences can
be shared with other ropes and can be generated lazily, i.e., from IO or
other generators. All that is needed is the length of the subsequence.
Every internal node keeps track of its own size and the size of its left
child. Thus, the offset of a node in the tree is the size of its left
child plus its ancestors. Ropes can be used to represent long strings
efficiently and many operations on ropes are O(1) where they are O(n) on
a string. This is offset by the fact that lookup in a rope is O(lg n)
versus O(1) for a string, but in many cases this isn't a problem.

Anyway, the rope data structure is further described in [1]. Boehm has
actually implemented this in C for his garbage collector, so see that
package for an example implementation (not though that it uses a lot of
C-hacks which makes it undesirable to use as-is). There's also a rope
data structure in STL, but it's limited to only using ropes and strings,
not IO,
nikolai (the rope and piece table lover)

[1] Hans-J Boehm, "Ropes: an Alternative to Strings", Software--Practice
and Experience, vol. 25(12), 1315--1330, Dec. 1995. Available at
http://rubyurl....

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Hal E. Fulton

3/17/2005 12:40:00 AM

0

Peter C. Verhage wrote:
> But why do Strings not behave like Symbols? I mean, why aren't all
> Strings immutable? Is this because Symbols will never get garbage
> collected (to make sure they can be used over and over again) and normal
> Strings will? Which might mean that in some cases (lots of text
> processing) immutable Strings would fill up memory?

Some people (such as Guido) dislike mutable strings.
Others (such as Matz, and incidentally me) like them.

Personally, my limited Java experience juggling String and
StringBuffer was enough to convince me that strings should
be mutable.


Hal



Douglas Livingstone

3/17/2005 1:10:00 AM

0

On Thu, 17 Mar 2005 05:37:12 +0900, Sebestyén Gábor <segabor@chello.hu> wrote:
> I like string keys

Why?

Personally I think :symbols are great, makes it much clearer when you
are reading code that you are representing something else, rather than
storing a piece of data. And you can use them without having to define
them as constants before hand. Great :)

Faster to type too.

Douglas