Ruby - Hashes Within Hashes

Introduction

You can have hashes within hashes.

Demo

people = { 
 'xml' => { # www  .  j a  v  a2s.com
     'name' => 'XML', 
     'age' => 3, 
     'gender' => 'male', 
     'favorite painters' => ['Java', 'C', 'C++'] 
 }, 
 'json' => { 
     'name' => 'JSON', 
     'age' => 5, 
     'gender' => 'female' 
 } 
} 

puts people['xml']['age'] 
puts people['json']['gender'] 
p people['json'] 

puts people['xml']['favorite painters'].length 
puts people['xml']['favorite painters'].join(", ")

Result

Related Topic