3

Possible Duplicates:
Why don't more projects use Ruby Symbols instead of Strings?
What's the difference between a string and a symbol in Ruby?

I am new to ruby language. From what I read I must use symbols instead strings where is possible. Is that correct ?

Community
  • 1
  • 1
adaxa
  • 1,580
  • 2
  • 14
  • 17
  • Must use? where are you reading this? Symbols are lighter weight, so in a lot of cases it is preferable that you use them.. – Doon Dec 10 '10 at 20:25
  • Just another thing to keep in mind: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/164890 Also read the accompanying discussion on SO: http://stackoverflow.com/questions/659755/ruby-symbols-are-not-garbage-collected-then-isnt-it-better-to-use-a-string – Michael Kohl Dec 11 '10 at 00:01

4 Answers4

5

You don't HAVE TO do anything. However, it is advisable to use symbols instead of strings in cases where you will be using the same string literal over and over again. The reason is that there is only ever 1 of the symbol held in memory, while if you use the string literal it will created anew whenever you assign it, therefore potentially wasting memory.

ennuikiller
  • 46,381
  • 14
  • 112
  • 137
4

Strings are mutable where as Symbols are not. Symbols are better performance wise.

Read this article to better understand symbols, strings and their differences.

1

It's sort of up to you which one you use -- you can use a string anywhere you'd use a symbol, but not the other way around. Symbols do have a number of advantages, in a few cases.

Symbols give you a performance advantage because two symbols of the same name actually map to the same object in memory, whereas two strings with the same characters create different objects. Symbols are immutable and lightweight, which makes them ideal for elements that you won't be changing around at runtime; keys in a hash table, for example.

Here's a nice excerpt from the Ruby Newbie guide to symbols:

The granddaddy of all advantages is also the granddaddy of advantages: symbols can't be changed at runtime. If you need something that absolutely, positively must remain constant, and yet you don't want to use an identifier beginning with a capital letter (a constant), then symbols are what you need.

The big advantage of symbols is that they are immutable (can't be changed at runtime), and sometimes that's exactly what you want.

Sometimes that's exactly what you don't want. Most usage of strings requires manipulation -- something you can't do (at least directly) with symbols.

Another disadvantage of symbols is they don't have the String class's rich set of instance methods. The String class's instance method make life easier. Much easier.

Sam Ritchie
  • 10,988
  • 4
  • 42
  • 53
0

Symbols have two main advantages:

  • They are like intern'ed strings, so they can improve performance.
  • They are syntactically distinct which can make your code more readable, particularly for programs that use hashtables as complex build-in data structures.
Stephen Petschulat
  • 1,159
  • 2
  • 14
  • 23