# Immutable, Interned strings?

**URL:** <https://www.ruby-forum.com/t/immutable-interned-strings/146135>\
**Category:** Ruby\
**Created:** [August 18, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/immutable-interned-strings/146135 "2008-08-18T12:14:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [August 18, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/immutable-interned-strings/146135/1 "2008-08-18T12:14:48Z")

</div>

Hello,

I’m learning Ruby via the “The Ruby P.ming Language”.  
The first chapter has the following statements;

Ruby’s hashes can use any object as a key, but Symbol objects are the  
most commonly used.  
Symbols are immutable, interned strings.

What is the meaning of immutable, interned strings?  
Would like to know in common, general English 🙂

Thanks,

~Mayuresh

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [August 18, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/immutable-interned-strings/146135/2 "2008-08-18T12:25:21Z")

</div>

Mayuresh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> What is the meaning of immutable, interned strings?  
> Would like to know in common, general English 🙂

“immutable” means “unchangeable” - you cannot change the value of a  
symbol. “interned” means (vaguely) that all symbols with the same name  
refer to the same symbol value, more or less like a global constant.

IOW, the symbol :this will always refer to the same object anywhere in  
your program meaning comparisons between symbols can be done using  
very efficient pointer comparisons instead of comparing each character  
of a string, so, for instance:

def is\_this?(sym)  
sym === :this;  
end

is\_this?(:this) =\> true

HTH,  
Joost.

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [August 18, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/immutable-interned-strings/146135/3 "2008-08-18T12:28:53Z")

</div>

> What is the meaning of immutable, interned strings?  
> Would like to know in common, general English 🙂

Well, immutable means unchangeable. A String object can have its value  
changed:

irb(main):001:0\> s = “foo”  
=\> “foo”  
irb(main):002:0\> s.object\_id  
=\> -605872668  
irb(main):003:0\> s.hash  
=\> 876516207  
irb(main):004:0\> s.gsub! ‘foo’, ‘bar’  
=\> “bar”  
irb(main):005:0\> s  
=\> “bar”  
irb(main):006:0\> s.object\_id  
=\> -605872668  
irb(main):007:0\> s.hash  
=\> 841490416

You can see here that the object ID does not change, s is still the same  
String object. You cannot change the value of a Symbol in this way –  
its  
value is fixed once the object is created.

The ‘hash’ method is used by Hash objects to convert key objects to  
numbers,  
used to index the hashtable. The hash of an object generally depends on  
its  
properties, and two equal objects must have the same hash. Since the  
values  
of Symbols cannot change, neither can their hash codes.

I think ‘interned’ has to do with the fact that, since Symbols never  
change,  
they are stored in a pool in memory and reused where possible. So, for  
example, all symbol literals :foo in a program all refer to the same  
Symbol  
object in memory, since there’s no point creating additional, identical  
Symbols.

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [August 20, 2008, 7:04am UTC](https://www.ruby-forum.com/t/immutable-interned-strings/146135/4 "2008-08-20T07:04:45Z")

</div>

On Monday 18 August 2008 05:10:58 Mayuresh K. wrote:

> Hello,

> What is the meaning of immutable, interned strings?  
> Would like to know in common, general English 🙂

Others have explained, mechanically. In terms of usage, you would use  
this in  
at least a few places you might otherwise use an Enum or a global  
constant in  
other languages.

For example, suppose you have a function which can do three slightly  
different  
things. Say, for the sake of argument, it generates an input field,  
which can  
be a checkbox, a dropdown, or a text field. You could call it like this:

field :checkbox  
field :dropdown  
field :text

And the function might look like this:

def field(type)

# do some stuff common to all types

if type == :checkbox  
# do something special for the checkbox  
end

# more common stuff…

… you get the idea.

And yes, they’re used widely as hash keys – in particular, to make up  
for the  
fact that Ruby doesn’t have named arguments.

But if you want to know how that works, just look at any Rails example  
code:

has\_many :accounts, :through =\> :customers

It’s that last bit that’s interesting, because you could always decide  
that  
you don’t want customers. You want zebras:

has\_many :zebras, :class =\> ‘Customer’  
has\_many :stripes, :through =\> :zebras, :class =\> ‘Account’

I don’t know if that will actually work, but you get the idea.
