0

like in php:

$input = [
      'a' => 'A',
      'b' => 'B',
      'cdef' => [
             'c' => 'C',
             'd' => 'D',
             'ef' => [
                  'e' => 'E',
                  'f' => 'F'
             ]
      ]
]

maybe use Hash? never used ruby before :)

When I worte code:

input = Hash.new
input['a'] = 'A'
input['b'] = 'B'
input['cdef']['c'] = 'C'
input['cdef']['d'] = 'D'
input['cdef']['ef']['e'] = 'E'
input['cdef']['ef']['f'] = 'F'

error at

  input['cdef']['c'] = 'C'

message :

[]=' for nil:NilClass
1
  • 1
    input = { 'a' => 'A', 'b' => 'B', 'cdef' => { 'c' => 'C', ... } }. Commented Sep 21, 2016 at 2:56

2 Answers 2

3

Although the answer by @davidhu2000 is more or less correct, I would go with more robust solution: using default_proc in the constructor. The dup.clear magic is to recursively pass the default_proc through to deeply nested elements:

input = Hash.new { |h, k| h[k] = h.dup.clear }
input['a'] = 'A' 
input['b'] = 'B' 
input['cdef']['c'] = 'C' 
input['cdef']['d'] = 'D' 
input['cdef']['ef']['e'] = 'E' 
input['cdef']['ef']['f'] = 'F' 

input

That way one does not need ugly redundant assignments:

input['cdef'] = {}
input['cdef']['ef'] = {} 

Ninja assignment:

input = Hash.new { |h, k| h[k] = h.dup.clear }
input['a1']['a2']['a3']['a4']['a5'] = 42
input
#⇒ {"a1" => {"a2" => {"a3" => {"a4" => {"a5" => 42}}}}}
Sign up to request clarification or add additional context in comments.

5 Comments

@SergioTulentsev thanks. It costs producing temporary redundant copy of the initial (and all nested having children) hash, though. But on human-operated hashes it should work fine.
@mudasobwa thx!... but how do I use this trick after I copy a clone of another multidimentional hash into input,like I have origin = {'a'=>1,'b'=>2,'cd'=>{'c'=>3,'d'=>4}}, and I copy origin into input,using input = origin.clone, then ur thick doesn't work,maybe clone operation has ruin the structure of input?
In the first place, please stop calling hashes “multidimentional arrays,” since they are not arrays by any means and php just implied a wrong usage of this term. Secondary, I did not understand the problem. As origin hash has default_proc declared, it’s clone has is either. The last, but not least: you should’ve asked another question on that, whether you have one.
f, I called it "multidimentional hash",since u haven't go through my question carefully with respect, I'm ok with it , s, thx, I know where the point is, lbnl, my question has not been solved, why can't I ask more, MDZZ
Indeed, there was a “multidimentional hash," nevertheless, it’s a plain old good “hash.” Please also use English, since there is not a chatroom for 14-yo cheerleaders. Your problem has been solved, now you have another one about cloning hashes—go ask it. SO is tended to implement a model 1 question per posting to make it easier for future readers to narrow their problem amongst the variety of already existing answers.
1

To fix your error, you need to initialize an empty hash before assigning a key-value pair.

input = Hash.new
input['a'] = 'A'               #=> {"a"=>"A"}
input['b'] = 'B'               #=> {"a"=>"A", "b"=>"B"}
input['cdef'] = {}             #=> {"a"=>"A", "b"=>"B", "cdef"=>{}}
input['cdef']['c'] = 'C'       #=> {"a"=>"A", "b"=>"B", "cdef"=>{"c"=>"C"}}
input['cdef']['d'] = 'D'       #=> {"a"=>"A", "b"=>"B", "cdef"=>{"c"=>"C", "d"=>"D"}}
input['cdef']['ef'] = {}       #=> {"a"=>"A", "b"=>"B", "cdef"=>{"c"=>"C", "d"=>"D", "ef"=>{}}}
input['cdef']['ef']['e'] = 'E' #=> {"a"=>"A", "b"=>"B", "cdef"=>{"c"=>"C", "d"=>"D", "ef"=>{"e"=>"E"}}}
input['cdef']['ef']['f'] = 'F' #=> {"a"=>"A", "b"=>"B", "cdef"=>{"c"=>"C", "d"=>"D", "ef"=>{"e"=>"E", "f"=>"F"}}}

2 Comments

I wonder how will assignment of nested element on level 10 look like with this approach. Please check my answer.
it'll be ugly, i definitely like your solution better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.