I have an array of Item objects that each have a string particular and a numeric amount.  I want to put all particulars  (separated by newlines) into a single string and the same for all amounts.Here's what I came up with:particulars = []amounts = []items.each do |item|  particulars << item.particular  amounts << item.amountendparticulars_string = particulars.join(\n)amounts_string = amounts.join(\n)So ifitem1.particular = fooditem2.particular = drinkitem1.amount = 1000item2.amount = 2000then running the code above givesparticulars_string # food\ndrinkamounts_string # 1000\n2000which is correct.  However, I feel that the above code can be done better.  In particular, I want all the code in one loop, not the three (each and two joins) I have now.  What's a better way to do this?
Processing array of objects into two strings
ruby;strings;array
You can get by with just two lines:particulars_string = items.map(&:particular).join(\n)amounts_string     = items.map(&:amount).join(\n)Enumerable#map, which is mixed into Array, creates a new array from an existing array by running a block on each element and storing the result. In this case though you don't need a full block, since you just need to call a single method (particular or amount). So it basically extracts those into new arrays and then joins those arrays.As a rule of thumb, you almost never have to manually map stuff from one array to another in Ruby using each and <<. Be sure to read the docs for Array and Enumerable as they're full of good stuff.