285

When I say { :bla => 1, :bloop => 2 }, what exactly does the : do? I read somewhere about how it's similar to a string, but somehow a symbol.

I'm not super-clear on the concept, could someone enlighten me?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
  • 1
    possible duplicate of [Understanding Symbols In Ruby](http://stackoverflow.com/questions/2341837/understanding-symbols-in-ruby) – Mladen Jablanović Jun 14 '11 at 05:24
  • 7
    Try looking at this: [The Ruby_Newbie Guide to Symbols](http://www.troubleshooters.com/codecorn/ruby/symbols.htm) – Hengjie Mar 12 '13 at 02:44
  • [This video](https://www.youtube.com/watch?v=mBXGBbEbXZY) tells you everything you need to know about symbols. – totymedli Jun 15 '18 at 21:45

9 Answers9

296

:foo is a symbol named "foo". Symbols have the distinct feature that any two symbols named the same will be identical:

"foo".equal? "foo"  # false
:foo.equal? :foo    # true

This makes comparing two symbols really fast (since only a pointer comparison is involved, as opposed to comparing all the characters like you would in a string), plus you won't have a zillion copies of the same symbol floating about.

Also, unlike strings, symbols are immutable.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 3
    just wonder why literal string does not support string interning ? – onmyway133 Sep 19 '14 at 04:55
  • 6
    @onmyway133 Because Ruby's strings are mutable. Interning only applies to immutable values. – C. K. Young Sep 19 '14 at 05:35
  • 4
    a) Why is `"foo".equal? "foo"` false? b) Can you refer to a symbol anywhere, essentially making them like global variables? – Arc676 Feb 20 '16 at 09:11
  • 4
    @Arc676 1. `equal?` in Ruby does identity comparison. Each string literal, like `"foo"`, creates a new string instance. It works that way because strings in Ruby are mutable. 2. Symbols are global, but more like global constants than global variables, since symbols do not have state. Thus using symbols is not an antipattern in the way that global variables are. – C. K. Young Feb 20 '16 at 15:11
  • 5
    @Arc676 `"foo" == "foo"` # => true – Filip Bartuzi May 10 '17 at 13:58
58

Just to demonstrate some of the things mentioned in the answers:

require 'benchmark'

n = 1_000_000

print '"foo".equal? "foo" -> ', ("foo".equal? "foo"), "\n"
print '"foo" == "foo"     -> ', ("foo" == "foo"    ), "\n"
print ':foo.equal? :foo   -> ', (:foo.equal? :foo  ), "\n"
print ':foo == :foo       -> ', (:foo == :foo      ), "\n"

Benchmark.bm(10) do |b|
  b.report('string')     { n.times { "foo".equal? "foo" }}
  b.report('str == str') { n.times { "foo" == "foo"     }}
  b.report('symbol')     { n.times { :foo.equal? :foo   }}
  b.report('sym == sym') { n.times { :foo == :foo       }}
end

Running it outputs:

"foo".equal? "foo" -> false
"foo" == "foo"     -> true
:foo.equal? :foo   -> true
:foo == :foo       -> true

So, comparing a string to a string using equal? fails because they're different objects, even if they are equal content. == compares the content, and the equivalent checks with symbols are much faster.

                 user     system      total        real
string       0.370000   0.000000   0.370000 (  0.371700)
str == str   0.330000   0.000000   0.330000 (  0.326368)
symbol       0.170000   0.000000   0.170000 (  0.174641)
sym == sym   0.180000   0.000000   0.180000 (  0.179374)

Both symbol tests are basically the same as far as speed. After 1,000,000 iterations there's only 0.004733 second difference, so I'd say it's a wash between which to use.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Extremely helpful! On my system, `==` resulted faster than `.equal?` for both string and symbol comparisons. Symbol comparison resulted 3+ times faster than string comparisons. – melvynkim Mar 07 '14 at 12:44
34

Symbols are a way to represent strings and names in ruby.

The main difference between symbols and strings is that symbols of the same name are initialized and exist in memory only once during a session of ruby.

They are useful when you need to use the same word to represent different things

nunopolonia
  • 14,157
  • 3
  • 26
  • 29
24

There're some quotes from the famous book Agile Web Development with Rails, which may be helpful to understand the symbol as well :

Rails uses symbols to identify things. In particular, it uses them as keys when naming method parameters and looking things up in hashes.

redirect_to :action => "edit", :id => params[:id]

You can think of symbols as string literals that are magically made into constants. Alternatively, you can consider the colon to mean "the thing named", so :id is "the thing named id".

shyan1
  • 1,003
  • 15
  • 17
8

In ruby each object has a unique object identifier, if you write puts "hello".object_id in your irb and hit return for 2 different times, you will get 2 different returning value,but if you write :hello.object_id 2 times you will only get the same one returning value. That should have explained the difference.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Alfred Huang
  • 156
  • 2
  • 4
3

If you use :foo => bar, foo will be a symbol. The benefit to symbols is that they are unique. When you call on an item in the hash, you do hash[:foo].

Symbols require less memory than strings, which also makes them useful if you want to make your program a little faster.

Charles
  • 1,384
  • 11
  • 18
1

All these answers omit one extra tantalising detail.. if you stringify the symbol :foo, you get.. guess what.. the string "foo". Hence

irb(main):025:0>
irb(main):026:0> :foo
=> :foo
irb(main):027:0> "#{:foo}"
=> "foo"
irb(main):028:0>
irb(main):029:0> 'foo' <=> :foo
=> nil
irb(main):030:0> 'foo' <=> :foo.to_s
=> 0
irb(main):031:0>

Hence.. for Perl programmers.. it's Ruby's answer to the 'bare word'.

Frank Carnovale
  • 458
  • 4
  • 6
0

It's a symbol. Basically, you are saying that the two elements of the hash have keys bla and bloop, much as if you had used the strings "bla" and "bloop". However, they take up less memory than strings and are easier to type.

kindall
  • 178,883
  • 35
  • 278
  • 309
0

If you are familiar with Java, you might be aware that Strings in Java are immutable. Symbols are similar in that sense in Ruby. They are immutable, i.e., any number of occurances of a particular symbol :symbol will map to only a single memory address. And, hence, it is recommended to use symbols wherever possible since it optimizes memory usage.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Dhruva Sagar
  • 7,089
  • 1
  • 25
  • 34
  • 1
    The fact that symbols are immutable ensures that they are always the same instance throughout your application and hence they are guaranteed to be the same object. Check these references : http://www.troubleshooters.com/codecorn/ruby/symbols.htm http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/ You'll find loads more if you google. – Dhruva Sagar Aug 21 '12 at 07:03
  • I am talking about your analogy to Java. Java Strings are not analogous to symbols. Java string literals are but not all strings. – smartnut007 Aug 21 '12 at 19:25
  • Perhaps my statement wasn't clear enough. They are analogous to each other only with respect to the fact that they are immutable. – Dhruva Sagar Aug 22 '12 at 10:47
  • @DhruvaSagar: The analogy would be better if you used Objective C’s `NSString`. There `"foo"` will always be equal to `"foo"`, because internally strings that are the same are just pointed to. The answer would still be confusing, though. – Rafael Bugajewski Jan 04 '16 at 10:47