Sei sulla pagina 1di 3

Ruby Learning through Examples

Symbols

Symbol is a class and the objects represent names & strings inside
the Ruby Interpreter.

Symbols are created by prefixing “:” colon to any variable or string


with-in literals

:sample_symbol # Prefixing to names

:”This is sample symbol” # Prefixing colon to string in


literals

If the names are simple then prefixing colon would be apt and the
literal option will be handy in case the symbols are lengthy strings.

Apart from the above there is a method “to_sym” that will convert
to symbols.

“This is string”.to_sym # :”This is string”


“sample_symbol”.to_sym # :sample_symbol

Symbol class consists of following set of methods

<=> == === =~ [] all_symbols capitalize casecmp downcase e


mpty? encoding id2name inspect intern length match next size sli
ce succ swapcase to_proc to_s to_sym upcase

And includes the module – Comparable.

We can see the count of symbols that Ruby supports by using the
all_symbols method

Symbol.all_symbols.size # 2860

Note – The size is mentioned for the installed Ruby version 1.87.
This differs version to version

“sample_symbol”.to_sym # :sample_symbol

One can quickly raise a query why we would need Strings if they are
just representing strings! Why to use symbols?

For the benefit of all and easy/quicker understanding let’s learn this
step by step. Recollecting on Strings, Strings are nothing but
_______________________________________________________________
Page 1 of 3 Sumanth Krishna. A
Ruby Learning through Examples

collection of characters and they are mutable. That means the data
string holds subjected to change any time and interpreter can never
be aware of this change. So interpreter need to allocate memory in
case of the strings.

Let’s get into irb, and punch in the following line and repeat this 5
times

puts “This is string”.object_id #23839380

Repeating the same for 4-5 times, you should be able to notice
something like below. Note that the “object_id” reference differs
each time you punch in. This proves the point that the interpreter
needs to allocate the memory space as it does not know what data
the string is holding.

In case of symbols which are immutable once defined it will be


referred to the same.

Now try this…

:“This is string”.object_id # 30228

How many ever times you repeat the above, you would find that
object id is same and it’s pointing still the same space in the
memory.

When to use symbol?

_______________________________________________________________
Page 2 of 3 Sumanth Krishna. A
Ruby Learning through Examples

As we had noted that the symbol is effective in terms of memory


allocation and not stacking the heap with too many object id’s, it is
suggested to use Symbols as much as possible.

In Ruby on Rails, one would notice the usage of symbol more and
very effectively.

Not convinced yet! Let’s see some statistics. Copy & Paste the
below snippet as “symbol-string-benchmark.rb”

Executing this file you will be see the results as

String: 13.703
Symbol: 6.813

Try experimenting with different ranges and appreciate the


efficiency of symbols.

_______________________________________________________________
Page 3 of 3 Sumanth Krishna. A

Potrebbero piacerti anche