From: Vincent Fourmond Date: 2007-01-25T08:14:37+09:00 Subject: Re: can it be shorter? Dorren wrote: > doing access control on rails controller, > > ------ I have this input -------------- > hash = {"index" => "list", > ["edit", "update"] => "manage_one", > ["new", "create", "destroy"] => "manage_all"} > > ------- I want this output ---------- > {"index"=>"list", > "edit"=>"manage_one", > "update"=>"manage_one", > "new"=>"manage_all", > "create"=>"manage_all", > "destroy"=>"manage_all" > } > > ------ I have this code ------------ > hash = Hash[*hash.to_a.collect{|x| > Array === x[0] ? x[0].zip([x[1]]*x[0].size) : x > }.flatten] > > ----- I want shorter code ------ What about this: h = {} for k,v in hash [k].flatten.each {|l| h[l] = v} end hash = h > => {"new"=>"manage_all", "edit"=>"manage_one", "destroy"=>"manage_all", "create"=>"manage_all", "index"=>"list", "update"=>"manage_one"} Cheers, Vincent -- Vincent Fourmond, PhD student (not for long anymore) http://vincent.fourmond.neuf.fr/