Immutable, Interned strings??

M

Mayuresh Kathe

Hello,

I'm learning Ruby via the "The Ruby Programming 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
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]
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.
 
J

Joost Diepenmaat

Mayuresh Kathe said:
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.
 
D

David Masover

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top