#we know that way to create hash_in_hash
h=Hash.new{ |h,k| h[k]=Hash.new(&h.default_proc) }
#it reduce time to create both internal and external hashes.
#consider, hash={h1=>{h1h=>{w1=>k1,w2=>k2,…}},…,h5=>{h5h=>{…}}
#how can I iterate this hash? expecting easy way like second_line.
Dear Selvag,
I’m not understand well.
Could you give an example of a hash and how you want to iterate
through it (the results you are expecting)?
I’m asking this because to iterate through a Hash of this kind is
similar to a tree.
And when you iterate through a tree, there’s more than a way to do it
and sometimes people want to:
- get track of (what is) the current depth,
- the parent nodes of the current node,
- what would be the return value for current node after iterating
through the children nodes, - what should it be yield to the block? A (sub)tree of the childrens?
A return value? A return value of the children AND the current node? - and things like that.
Abinoam Jr.
consider this inheritance, where parent hash contains more user_id
each user_id has more date_id and each date_id has only one hash (name
may be “item_list”.
That is,
usr_id_1={dt_10={a=>1,b=>2,c=>3},dt_12=>{c=>3,d=>4}…}
usr_id_2={dt_8={aa=>11,cb=>12,cs=>33},dt_12=>{cc=>30,da=>41}…}
my_hash={usr_id_1, usr_id_2,…,usr_id_n}
iterate the contents of my_hash and output may be as:
usr_id | date_id | item | qty