[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Immutable, Interned strings??

Mayuresh Kathe

8/18/2008 10:11:00 AM

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

3 Answers

James Coglan

8/18/2008 10:23:00 AM

0

[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.

Joost Diepenmaat

8/18/2008 10:24:00 AM

0

Mayuresh Kathe <kathe.mayuresh@gmail.com> 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.

--
Joost Diepenmaat | blog: http://joost.... | work: http://...

David Masover

8/20/2008 5:00:00 AM

0

On Monday 18 August 2008 05:10:58 Mayuresh Kathe 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.