Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

ruby on rails - How do I loop over a hash of hashes?

I have this hash:

 h
 => {"67676.mpa"=>{:link=>"pool/sdafdsaff", :size=>4556}} 

>  h.each do |key, value|
>     puts key
>   puts value
>   end
67676.mpa
linkpool/sdafdsaffsize4556

How do I access the separate values in the value hash on the loop?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Value is a Hash to so you need iterate on it or you can get only values:-

h.each do |key, value|
  puts key
  value.each do |k,v|
    puts k
    puts v
  end
end

or

h.each do |key, value|
  puts key
  value.values.each do |v|
    puts v
  end
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...