From: Dave Aronson Date: 2012-10-14T00:43:15+09:00 Subject: Re: multiple array problem On Sat, Oct 13, 2012 at 7:35 AM, Roelof Wobben wrote: > category1.article.each do |artikel| > puts article.name > end > > category2.article.each do |artikel| > puts article.name > end > > category3.article.each do |artikel| > puts article.name > end > > But now I have 3 parts of code which are almost the same. > Is there a way I can make 1 part of code where the category is a variable. Sure. Make an array out of your 3 arrays: [category1, category2, category3]. Then call .each on THAT. E.g.: [category1, category2, category3].each do |category| category.article.each do |article| puts article.name end end Not sure where you're getting category.article from though; is category an instance of some class like Category, that has an array called article? (If so, I'd suggest making the name plural to show it's a collection.) Which reminds me, watch out for mismatches between human languages. What you originally wrote wouldn't work (at least not as intended), since you're using "artikel" as your loop variable, and "article" inside it. -Dave -- Dave Aronson, T. Rex of Codosaurus, LLC... aka Available Secret-Cleared Ruby/Rails Freelancer (NoVa/DC/Remote); see http://www.Codosaur.us/.