我有一些简单的 ruby 代码,我在其中创建了一个数组,创建了一堆对象并将它们插入数组,然后返回数组
def create_barcodes(count)
barcodes = Array.new
count.times { barcodes.push(Barcode.create) }
barcodes
end
感觉应该有一种方法可以将其减少到一两行,并且至少避免必须在末尾引用 barcodes
数组,以便它被返回。有没有一些巧妙的方法让计数循环返回数组?
最佳答案
barcodes = Array.new(count){ Barcode.create }
关于arrays - 我可以在循环中将对象添加到数组,然后在同一行中返回数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50541349/