Does String cache its 'hash' return value?

A

Asfand Yar Qazi

What I mean is, if I repeatedly call a String's hash method (i.e. like
using it regularly to access a Hash) will the hash value get
recalculated every time?
 
F

Florian Frank

What I mean is, if I repeatedly call a String's hash method (i.e. like
using it regularly to access a Hash) will the hash value get
recalculated every time?

Yes, it will. Have you considered unsing Symbols instead of Strings?
The hash value "calculation" of a Symbol is O(1).
 
A

Asfand Yar Qazi

Florian said:
Yes, it will. Have you considered unsing Symbols instead of Strings?
The hash value "calculation" of a Symbol is O(1).

Depends... if Symbols are used as the keys for a hash table, can they be
as easily accessed from extension modules as if Strings were used?
 
G

gabriele renzi

il Wed, 24 Dec 2003 00:11:27 +0000, Asfand Yar Qazi
Depends... if Symbols are used as the keys for a hash table, can they be
as easily accessed from extension modules as if Strings were used?

why the should'nt ? Many lybraries make use of symbols as constants,
ruby-gtk or Pdf::Writer are examples.. and, btw "everything is an
object" make everything work as everything else :)
 
F

Florian Frank

Depends... if Symbols are used as the keys for a hash table, can they be
as easily accessed from extension modules as if Strings were used?

You can use ID2SYM(rb_intern("foo")) to get the VALUE of symbol :foo. I
don't think it's too complicated.
 
N

nobu.nokada

Hi,

At Tue, 23 Dec 2003 09:56:53 +0900,
Asfand said:
What I mean is, if I repeatedly call a String's hash method (i.e. like
using it regularly to access a Hash) will the hash value get
recalculated every time?

Hash (accurately, st library) has cache, but String doesn't.
 
A

Asfand Yar Qazi

Florian said:
You can use ID2SYM(rb_intern("foo")) to get the VALUE of symbol :foo. I
don't think it's too complicated.

WOW DAT WAS KWICK U R LEE7!!!11111
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top