Does String cache its 'hash' return value?

Discussion in 'Ruby' started by Asfand Yar Qazi, Dec 23, 2003.

  1. 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?






    --
    http://www.it-is-truth.org/
    Asfand Yar Qazi, Dec 23, 2003
    #1
    1. Advertising

  2. On Tue, 2003-12-23 at 01:56, Asfand Yar Qazi wrote:
    > 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).

    --
    o=lambda{|o|p o};O=Struct.new:)a,:b,:c);e=%q(_(?h,_(?h,_(?\ ,_(?s,_(?u,_(74)),
    _(?t)),_(?t,_(?o,_(?n,_(?a))))),_(82,_(?r,_(?e),_(32)),_(32,_(98,_(?u),_(?y)))
    )),_(?r,_(99,_(97),_(?k,nil,_(?e))),_(10))));def _(*a)O.new(*a)end;class O;def
    e(&o)b&&b.e(&o);o[a];c&&c.e(&o)end;end;def p(o)print(''<<o)end;eval(e).e(&o)
    Florian Frank, Dec 23, 2003
    #2
    1. Advertising

  3. Florian Frank wrote:
    > On Tue, 2003-12-23 at 01:56, Asfand Yar Qazi wrote:
    >
    >>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).
    >


    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?







    --
    http://www.it-is-truth.org/
    Asfand Yar Qazi, Dec 24, 2003
    #3
  4. il Wed, 24 Dec 2003 00:11:27 +0000, Asfand Yar Qazi
    <im_not_giving_it_here@i_hate_spam.com> ha scritto::


    >>
    >> 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?


    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 :)
    gabriele renzi, Dec 24, 2003
    #4
  5. On Wed, 2003-12-24 at 01:06, Asfand Yar Qazi wrote:
    > 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.

    --
    o=lambda{|o|p o};O=Struct.new:)a,:b,:c);e=%q(_(?h,_(?h,_(?\ ,_(?s,_(?u,_(74)),
    _(?t)),_(?t,_(?o,_(?n,_(?a))))),_(82,_(?r,_(?e),_(32)),_(32,_(98,_(?u),_(?y)))
    )),_(?r,_(99,_(97),_(?k,nil,_(?e))),_(10))));def _(*a)O.new(*a)end;class O;def
    e(&o)b&&b.e(&o);o[a];c&&c.e(&o)end;end;def p(o)print(''<<o)end;eval(e).e(&o)
    Florian Frank, Dec 24, 2003
    #5
  6. Asfand Yar Qazi

    Guest

    Hi,

    At Tue, 23 Dec 2003 09:56:53 +0900,
    Asfand Yar Qazi wrote:
    > 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.

    --
    Nobu Nakada
    , Dec 24, 2003
    #6
  7. Florian Frank wrote:
    > On Wed, 2003-12-24 at 01:06, Asfand Yar Qazi wrote:
    >
    >>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.
    >


    WOW DAT WAS KWICK U R LEE7!!!11111





    --
    http://www.it-is-truth.org/
    Asfand Yar Qazi, Dec 24, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greenhorn
    Replies:
    15
    Views:
    783
    Keith Thompson
    Mar 6, 2005
  2. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  3. thunk
    Replies:
    1
    Views:
    286
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    444
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    588
    thunk
    Apr 3, 2010
Loading...

Share This Page