Symbol in Ruby

Describe the reason why it is not preferrable to dynamically create a lot of symbols in Ruby.

From the two solutions below, which one explains the function of symbol in Ruby and describes the difference between string and symbol more precisely? Explain your answer.

Solution A
# Symbols are like strings but they are immutable - they can't be modified.
# They are only put into memory once, making them very efficient to use for things like keys in hashes but they stay in memory until the program exits. This makes them a memory hog if you misuse them.
# If you dynamically create lots of symbols, you are allocating a lot of memory that can't be freed until your program ends. You should only dynamically create symbols (using string.to_sym) if you know you will:
# need to repeatedly access the symbol
# not need to modify them
# As I said earlier, they are useful for things like hashes - where you care more about the identity of the variable than its value. Symbols, when correctly used, are a readable and efficient way to pass around identity.
# I will explain what I mean about the immutability of symbols RE your comment.
# Strings are like arrays; they can be modified in place:
12:17:44 ~$ irb
irb(main):001:0> string = "Hello World!"
=> "Hello World!"
irb(main):002:0> string[5] = 'z'
=> "z"
irb(main):003:0> string
=> "HellozWorld!"
# Symbols are more like numbers; they can't be edited in place:
irb(main):011:0> symbol = :Hello_World
=> :Hello_World
irb(main):012:0> symbol[5] = 'z'
NoMethodError: undefined method `[]=' for :Hello_World:Symbol
    from (irb):12
    from :0
Solution B
# A symbol is the same object and the same allocation of memory no matter where it is used:
>> :hello.object_id
=> 331068
>> a = :hello
=> :hello
>> a.object_id
=> 331068
>> b = :hello
=> :hello
>> b.object_id
=> 331068
>> a = "hello"
=> "hello"
>> a.object_id
=> 2149256980
>> b = "hello"
=> "hello"
>> b.object_id
=> 2149235120
>> b = "hell" + "o"
# Two strings which are 'the same' in that they contain the same characters 
# may not reference the same memory, which can be inefficient 
# if you're using strings for, say, hashes.
# So, symbols can be useful for reducing memory overhead. However - 
# they are a memory leak waiting to happen, because symbols cannot be garbage 
# collected once created. Creating thousands and thousands of symbols would allocate
#  the memory and not be recoverable. Yikes!
Log in to accept this challenge
Submitted by others
  • Each step is clear. comments unneccessary

  • The preamble in solution A is slightly more pleasant to read, but the irb sequence in B is more helpful.