Q:

How to group a hash?

I got a hash (see below) that I need to group but I have no idea how to do it.
I will use this array for table sections in iOS (Rubymotion).
The hash looks like this and I want to group by profession:
[{"firstname" => "John", "lastname" => "Wayne", "profession" => "actor"},{"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"},{"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}]

I need it to look like this:
["actor" => [{"firstname" => "John", "lastname" => "Wayne", "profession" => "actor"},{"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"}], "director" =>[{"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}]

Update
It would be really good if I could get this structure:
{
    "profession" => "actor",
    "people" =>
    [
      {"firstname"=>"John", "lastname"=>"Wayne", "profession"=>"actor"},
      {"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"}
    ],
"profession" => "director",
    "people" =>
    [
      {"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}
    ]
  }

A:

Do as below using Enumerable#group_by :
a = [ 
        {"firstname" => "John", "lastname" => "Wayne", "profession" => "actor"},
        {"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"},
        {"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}
    ]

a.group_by { |h| h["profession"] }

output :
{  "actor"=> [
             {"firstname"=>"John", "lastname"=>"Wayne", "profession"=>"actor"}, 
             {"firstname"=>"Oliva", "lastname"=>"Newton", "profession"=>"actor"}
            ], 
    "director"=> [
             {"firstname"=>"Terry", "lastname"=>"Gilliam", "profession"=>"director"}
                 ]
}

update ( As @mdesantis  mentioned, your expected output is not the correct one, so I did change the structure to something meaningful.
a = [ 
        {"firstname" => "John", "lastname" => "Wayne", "profession" => "actor"},
        {"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"},
        {"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}
    ]

result_hash = a.group_by { |h| h["profession"] }.map do |key,val|
    {"profession" => key, "people" => val}
end

p result_hash

output
[
    {"profession"=>"actor", 
    "people"=> [ {"firstname"=>"John", "lastname"=>"Wayne", "profession"=>"actor"},
                 {"firstname"=>"Oliva", "lastname"=>"Newton", "profession" =>"actor"}
                ]
    },
    {"profession"=>"director",
    "people"=>[ {"firstname"=>"Terry", "lastname"=>"Gilliam", "profession"=>"director"}
              ]
    }
]

