I Need Help in writing script which help me cleaning hash of hashes before saving it. Which ever level title is empty it need to be deleted. Below is Example hash.
Before running Script
Questions= {
"0"=>
{
"title"=>"Question checkbox",
"id"=>"1",
"options_attributes"=>
{
"0"=>{"title"=>"1", "id"=>"1"},
"1"=>{"title"=>"2", "id"=>"2"}
},
"question_type_id"=>"4"
},
"1"=>
{
"title"=>"Question Radio",
"id"=>"2",
"options_attributes"=>
{
"0"=>
{
"title"=>"yes", "id"=>"3"
},
"1"=>
{
"title"=>"no", "id"=>"4"
},
"2"=>
{
"title"=>"", "id"=>""
}
},
"question_type_id"=>"3"
},
"2"=>
{
"title"=>"",
"options_attributes"=>
{
"0"=>{"title"=>"", "id"=>""},
"1"=>{"title"=>"", "id"=>""}
},
"question_type_id"=>"1"
}
}
After Running script Required Result
Questions= {
"0"=>
{
"title"=>"Question checkbox",
"id"=>"1",
"options_attributes"=>
{
"0"=>{"title"=>"1", "id"=>"1"},
"1"=>{"title"=>"2", "id"=>"2"}
},
"question_type_id"=>"4"
},
"1"=>
{
"title"=>"Question Radio",
"id"=>"2",
"options_attributes"=>
{
"0"=>
{
"title"=>"yes", "id"=>"3"
},
"1"=>
{
"title"=>"no", "id"=>"4"
}
},
"question_type_id"=>"3"
},
}
Here’s a script that prunes your tree of any hashes that contain “title” => “”.