Is there a way in Ruby to pass a hash value from string (or whatever) to
an Array dinamically?
Example: An array of objects Teacher, each with his name and belonging
school. After iterating though its elements, we want an output of the
type, “school_name_A” => “teacher_name_A”, “school_name_B” =>
[“teacher_name_B”, “teacher_name_C”].
…or maybe there is another simpler way of doing this?
Is there a way in Ruby to pass a hash value from string (or whatever) to
an Array dinamically?
Example: An array of objects Teacher, each with his name and belonging
school. After iterating though its elements, we want an output of the
type, “school_name_A” => “teacher_name_A”, “school_name_B” =>
[“teacher_name_B”, “teacher_name_C”].
It’s easier if you allow:
Example: An array of objects Teacher, each with his name and belonging
school. After iterating though its elements, we want an output of the
type, “school_name_A” => “teacher_name_A”, “school_name_B” =>
[“teacher_name_B”, “teacher_name_C”].
hash = Hash.new {|h,k| h[k] = []}
teachers.each do |teacher|
hash[teacher.school] << teacher.name
end