# Symbols and strings

**URL:** <https://www.ruby-forum.com/t/symbols-and-strings/112019>\
**Category:** Ruby\
**Created:** [August 14, 2007, 10:57am UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019 "2007-08-14T10:57:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maghac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@maghac](https://www.ruby-forum.com/u/maghac)\
**Post date:** [August 14, 2007, 10:57am UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019/1 "2007-08-14T10:57:14Z")

</div>

Hi,  
as a long-term perl user just recently converted to ruby, I’m curious  
about one particular syntax “feature”: ruby symbols and how they  
relate to strings.

Isn’t really :name a shortcut for “name”? (I read somewhere in an  
explanation of attr\_reader that e.g :age was the “name” of the  
variable age, while age is the actual content of that variable). If  
so, couldn’t you use this in hash keys as well, e.g say hashvar[:key]  
instead of hashvar[‘key’].

Are there situations where you cannot use symbols instead of strings,  
or the other way around?

I might be too used to the way strings and barewords are handled in  
perl (if something looks like a string, and it’s not a function call,  
it’s interpreted as a string. This means you can say $hashvar{key}  
without quoting the key).

---

<div class="post-metadata">

**Author:** ![maghac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@maghac](https://www.ruby-forum.com/u/maghac)\
**Post date:** [August 14, 2007, 11:17am UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019/2 "2007-08-14T11:17:17Z")

</div>

Alle martedì 14 agosto 2007, maghac ha scritto:

> Are there situations where you cannot use symbols instead of strings,  
> or the other way around?
> 
> I might be too used to the way strings and barewords are handled in  
> perl (if something looks like a string, and it’s not a function call,  
> it’s interpreted as a string. This means you can say $hashvar{key}  
> without quoting the key).

Symbols and strings are two different things. There are several  
situations in  
which you can pass a method either a symbol or a string, but this  
happens  
because who wrote the method took into account both possibilities. For  
instance, attr\_reader and similar, send, instance\_variable\_get and  
instance\_variable\_set, define\_method can take both a string or a symbol.  
Module#const\_get, instead, only accepts a symbol.

You can see that string and symbols are different using hashes:

h = { :a =\> 1}  
puts h[:a]  
=\> 1  
puts h[‘a’]  
=\> nil  
h[‘a’] = 2  
puts h[‘a’]  
=\> 2  
puts h[:a]  
=\> 1

There have been several threads on this topic. I suggest you look at  
them.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![maghac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@maghac](https://www.ruby-forum.com/u/maghac)\
**Post date:** [August 14, 2007, 11:12am UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019/3 "2007-08-14T11:12:16Z")

</div>

On 8/14/07, maghac [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Are there situations where you cannot use symbols instead of strings,  
> or the other way around?
> 
> I might be too used to the way strings and barewords are handled in  
> perl (if something looks like a string, and it’s not a function call,  
> it’s interpreted as a string. This means you can say $hashvar{key}  
> without quoting the key).  
> This question is asked very frequently, please search the archives for  
> more info.  
> I will try to give a very quick answer nevertheless:

Symbols are immutable, thus great to represent immutable data, often  
that makes them an excellent choice for hash keys  
{ :the\_value =\> 42, :alternatively =\> 22, :or\_even =\> 101010 }  
they just do not play the role of Strings, coming from Perl you just  
had to use Strings, you did not have a tool for names (wait a moment  
was there not such a thing as references to constants?).  
Very roughly put I see Symbols as name, and Strings as data, whenever  
I can use Symbols I use them, comes natural after some time.

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![maghac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@maghac](https://www.ruby-forum.com/u/maghac)\
**Post date:** [August 14, 2007, 11:21am UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019/4 "2007-08-14T11:21:17Z")

</div>

On Aug 14, 11:11 am, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > so, couldn’t you use this in hash keys as well, e.g say hashvar[:key]  
> > This question is asked very frequently, please search the archives for  
> > I can use Symbols I use them, comes natural after some time.
> 
> ## HTH Robert
> 
> […] as simple as possible, but no simpler.  
> – Attributed to Albert Einstein

Thanks, that cleared a few things for me.

I found something in the FAQ about it, but it didn’t really answer my  
question on the differences/similiarities between symbols and strings.

thanks  
Magnus

---

<div class="post-metadata">

**Author:** ![maghac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@maghac](https://www.ruby-forum.com/u/maghac)\
**Post date:** [August 14, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019/5 "2007-08-14T13:27:44Z")

</div>

maghac wrote:

> > > Isn’t really :name a shortcut for “name”? (I read somewhere in an  
> > > it’s interpreted as a string. This means you can say $hashvar{key}  
> > > had to use Strings, you did not have a tool for names (wait a moment
> 
> Thanks, that cleared a few things for me.
> 
> I found something in the FAQ about it, but it didn’t really answer my  
> question on the differences/similiarities between symbols and strings.
> 
> thanks  
> Magnus

Don’t make more out of this than there is. A symbol is a kind of literal  
in Ruby. It’s the name of a number. You pick the name, Ruby picks the  
number. Ruby guarantees that whenever you use the symbol in your program  
it will always refer to the same number.

That’s it. That’s all there is. Really.

Given a symbol, you can retrieve its name in the form of a string with  
the #to\_s method. You can convert a string (or almost any sequence of  
characters, for that matter) to a symbol with the #to\_sym method. Most  
methods that accept symbol arguments, like attr\_reader and kin, accept  
strings as well. All attr\_reader does with your symbol is use its name  
as the name of the attribute.

---

<div class="post-metadata">

**Author:** ![maghac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@maghac](https://www.ruby-forum.com/u/maghac)\
**Post date:** [August 14, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019/6 "2007-08-14T13:59:51Z")

</div>

Hi –

On Tue, 14 Aug 2007, Phlip wrote:

> the method?  
> $ ruby -ve ‘class \<\< :abc; end’  
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]  
> -e:1: no virtual class for Symbol (TypeError)

David

---

<div class="post-metadata">

**Author:** ![maghac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@maghac](https://www.ruby-forum.com/u/maghac)\
**Post date:** [August 14, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/symbols-and-strings/112019/7 "2007-08-14T13:45:48Z")

</div>

Tim H. wrote:

> Don’t make more out of this than there is. A symbol is a kind of literal  
> in Ruby. It’s the name of a number. You pick the name, Ruby picks the  
> number. Ruby guarantees that whenever you use the symbol in your program  
> it will always refer to the same number.

:foo.object\_id == :foo.object\_id

So if you add a method to one :foo, can anyone who invokes :foo access  
the method?
