From: Robert Klemme Date: 2010-11-03T02:30:14+09:00 Subject: Re: Printing out contents of 2 arrays using a loop On 02.11.2010 17:36, Rick DeNatale wrote: > On Tue, Nov 2, 2010 at 12:19 PM, Paul Roche wrote: >> Hi. I'd like to print out the contents of 2 arrays as follows........ >> >> print array1[0] array2[0] >> print array1[1] array2[1] >> etc >> >> array1 holds names >> array2 holds occupation >> >> so an example print out will be....... >> >> >> Joe Bloggs Carpenter >> etc >> >> >> I'm struggling constructing a loop or iteration that prints this out. > > names = %w(joe sam john fred) > occupations = %w(tinker tailor soldier spy) > > names.zip(occupations).each {|name, occupation| puts "#{name} is a > #{occupation}"} #zip accepts a block, which avoids creating the merged Array: irb(main):005:0> names.zip(occupations){|name, occupation| puts "#{name} is a #{occupation}"} joe is a tinker sam is a tailor john is a soldier fred is a spy => nil Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/