Can I add object to an array in a loop and then return the array in the same line?

  • A+
Category:Languages

I have some simple ruby code where I create an Array, create a bunch of objects and push them into the array and then return the array

def create_barcodes(count)   barcodes = Array.new   count.times { barcodes.push(Barcode.create) }   barcodes end 

It feels like there should be a way to reduce this to one or two lines and at a minimum avoid having to reference the barcodes array at the end so it gets returned. Is there some clever way to get the count loop to return the array?


barcodes = Array.new(count){ Barcode.create } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: