1

How I'm trying to build a hash for the following input

[["Company", "Add"], ["Company", "Edit"], ["Company", "Delete"], ["Company", "List"], ["Caterer", "Add"], ["Caterer", "Edit"], ["User", "Add"]] 

The output should be

[{'Company'=>['Add', 'List', 'Edit', 'Delete']},
 {'Caterer'=>['Add', 'List', 'Edit', 'Delete']},
 {'User'=>['Add']}]

Try 1:

input = [["Company", "Add"], ["Company", "Edit"], ["Company", "Delete"], ["Company", "List"], ["Caterer", "Add"], ["Caterer", "Edit"], ["User", "Add"]]
a=[]
input.each do |inp|
  tmp = Hash.new
  a<< tmp.update(inp[0] => inp[1])
end

result:

[{"Company"=>"Add"}, {"Company"=>"Edit"}, {"Company"=>"Delete"}, {"Company"=>"List"}, {"Caterer"=>"Add"}, {"Caterer"=>"Edit"}, {"User"=>"Add"}] 
4
  • Have you tried something? Share your efforts please. Commented Aug 20, 2015 at 14:04
  • 2
    What happened to "Create"? Could it be that your output is not matching because the input is also wrong? Commented Aug 20, 2015 at 14:12
  • @Зелёный check my result and from that is it possible to group key and values? Commented Aug 20, 2015 at 14:31
  • @FerdinandRosario yep, use group_by { |x| x.keys.first } Commented Aug 20, 2015 at 14:42

2 Answers 2

1
input = [["Company", "Add"], ["Company", "Edit"], ["Company", "Delete"], ["Company", "List"], ["Caterer", "Add"], ["Caterer", "Edit"], ["User", "Add"]]

as_hash = input.group_by(&:first)
as_hash.each do |entity, actions|
  as_hash[entity] = actions.flatten.reject do |action|
    action == entity
  end.sort_by { |action| ['Add', 'List', 'Edit', 'Delete'].index action }
end

EDIT: I just saw that you want an array of the pairs, instead of a hash. You can do that by:

as_hash.map { |pair| Hash[*pair] }
Sign up to request clarification or add additional context in comments.

3 Comments

same as below, output is differ with OP, also it's create a hash, but OP required an array of hashes
anyway, {"Company"=>["Add", "List", "Edit", "Delete"], "Caterer"=>["Add", "Edit"], "User"=>["Add"]} it's not the same as OP wanted. Caterer is wrong., also it create an one hash, but OP wanted, an array of hashes.
@Зелёный, fixed. Thanks again!
0

This should work

result={}
[["Company", "Create"], ["Company", "Edit"], ["Company", "Delete"], ["Company", "List"], ["Caterer", "Create"], ["Caterer", "Edit"], ["User", "Add"]].each do  |value|
   result[value[0]]=[] unless result.has_key? value[0]
   result[value[0]] << value[1]
end
puts result.to_s

{"Company"=>["Create", "Edit", "Delete", "List"], "Caterer"=>["Create", "Edit"], "User"=>["Add"]}

But is not an array of hashes

If you still want to get a array you could do this

r=result.map{|k,v| {k=>v} }

1 Comment

Caterer key is wrong, as like Company. {"Company"=>["Create", "Edit", "Delete", "List"], "Caterer"=>["Create", "Edit"], "User"=>["Add"]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.