a = ["Bob Green", "Don Brown", "Rich Freedom"]
a_hash = {"key_name" =>{"name" => "a_name", "a_thing" => 0}}
keys = a_hash.keys
for i in 0..a.length
  aCOG = a[i]
  aCOGkey = a[i].to_s.downcase.delete(' ')
  keys[i] = aCOGkey
  a_hash[keys[i]]["name"] = aCOG   #why does this line fail?
end
            Asked
            
        
        
            Active
            
        
            Viewed 109 times
        
    -3
            
            
         
    
    
        Sully
        
- 14,672
- 5
- 54
- 79
 
    
    
        user1701382
        
- 5
- 1
- 
                    4It is really hard to figure what are you trying to do here. Try explaining what was the intent and what failed, error message, etc. Also please see the [FAQ]. – Jakub Hampl Sep 26 '12 at 20:10
3 Answers
2
            
            
        Let's see:
keys[0] == aCOGkey == a[0] == "bobgreen"
Looks like that line fails because there's no key "bobgreen" in a_hash.
 
    
    
        the Tin Man
        
- 158,662
- 42
- 215
- 303
 
    
    
        Jesse Jashinsky
        
- 10,313
- 6
- 38
- 63
- 
                    
- 
                    It's been a while since I used Ruby, and I can't test it right now, but I'm pretty sure trying to access `name` before `a_hash[keys[i]]` is created causes it to crash. – Jesse Jashinsky Sep 26 '12 at 20:28
1
            
            
        Nested hash does not just create on its own. In another word, Ruby does not know the value to the key is a hash too.
You may use all methods above to specifically assign a hash, or you can use
a_hash = Hash.new { |h,k|  h[k] = {} }
This assumes all values in a_hash is a hash (nested hash). This method can set default value to a hash. You can also set h[k] = {name:"",a_thing:0} in your case.
So you only need to do like:
a = ["Bob Green", "Don Brown", "Rich Freedom"]    
a_hash = Hash.new { |h,k|  h[k] = {} }    
a.each{|n| a_hash[n.to_s.downcase.delete(' ')]["name"] = n}
Try not to use for in Ruby. Use enumerators instead.
Use symbols for hash keys.
 
    
    
        SwiftMango
        
- 15,092
- 13
- 71
- 136
- 
                    "Use symbols for hash keys", when it makes sense. Dogmatically trying to use symbols for hash keys can be frustrating because sometimes the key is too complex to be a symbol. – the Tin Man Sep 26 '12 at 23:07
- 
                    When it makes sense? Ref: http://stackoverflow.com/questions/8189416/why-use-symbols-as-hash-keys-in-ruby Also, it's never too complex to be a symbol if you know you can use a symbol in this way `:"this is a complex symbol"` – SwiftMango Sep 27 '12 at 13:45
- 
                    A hash key isn't necessarily a string. It can be any object. Try converting all the objects you can imagine into a symbol. They don't fit. So, while it's a good practice to use symbols for speed and memory, it's not always expedient and doesn't always lead to the most readable code, votes withstanding. – the Tin Man Sep 27 '12 at 15:37
- 
                    Thanks for the lesson. I think everyone knows that. That post explains really well of my point. EOC. – SwiftMango Sep 27 '12 at 16:14
0
            
            
        Writing a new key value pair to a hash inside a hash can work like this:
outer_hash['inner_hash_key']['new_key_in_inner_hash'] = 'String I want to add'.
 
    
    
        Thomas Klemm
        
- 10,678
- 1
- 51
- 54