#2 楼@luffycn
输入是什么样的呢?keys 是给定的吗?
变长是 ok 的。假如,输入是 arrys 和 keys:
one=['a','b','c']two=['A','B','C']three=['m','n','k','h']four=%w(x y )#假设输入为 arrays, keys:arrays=[one,two,three,four]keys=%w{one two three four}#orkeys=%w{one two three four five six seven ...}.first(arrays.size)#那么可以这样:klass=Struct.new(*keys.map(&:to_sym))objects=arrays.reduce(&:zip).map{|tup|klass.new(*tup.flatten)}.map(&:to_h)defhash_from_arr(arr)arr.reduce({})do|res,element|i=res.sizeres.merge!(i=>element)endendhash_from_arr(objects)