Why is a string key for a hash frozen?

前端 未结 4 1520
猫巷女王i
猫巷女王i 2021-02-07 00:52

According to the specification, strings that are used as a key to a hash are duplicated and frozen. Other mutable objects do not seem to have such special consideration. For exa

4条回答
  •  滥情空心
    2021-02-07 01:15

    See this thread on the ruby-core mailing list for an explanation (freakily, it happened to be the first mail I stumbled across when I opened up the mailing list in my mail app!).

    I've no idea about the first part of your question, but hHere is a practical answer for the 2nd part:

      new_hash = {}
      h.each_pair do |k,v|
       new_hash.merge!({k.downcase => v}) 
      end
    
      h.replace new_hash
    

    There's lots of permutations of this kind of code,

      Hash[ h.map{|k,v| [k.downcase, v] } ]
    

    being another (and you're probably aware of these, but sometimes it's best to take the practical route:)

提交回复
热议问题