我有两个数组:
a = [a1, ..., an]
b = [b1, ..., bn]
我想从这些数组中创建一个对象数组,其中一个对象具有字段a
和b
. 所以它看起来像:
o = [o1, ..., on]
在哪里o1.a = a1
和o1.b = b1
和o2.a = a2
等等o2.b = b2
。
现在,我有:
Obj = Struct.new(:a, :b)
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
objs = []
// Is there a better way of doing the following or is it okay?
a.zip(b).each do |ai|
objs << Obj.new(ai[0], ai[1])