irb(main):001:0> a1 = ["joe blogs", "peter smith"]
=> ["joe blogs", "peter smith"]
irb(main):002:0> a2 = ["carpenter", "policeman"]
=> ["carpenter", "policeman"]
irb(main):003:0> a1.zip(a2).each {|name, occupation| puts "#{name} is
a #{occupation}"}
joe blogs is a carpenter
peter smith is a policeman
Another way that avoids creating the intermediate array would be to
iterate using the index:
irb(main):004:0> a1.each_with_index {|name, i| puts "#{name} is a #{a2[i]}"}
joe blogs is a carpenter
peter smith is a policeman
=> ["joe blogs", "peter smith"]
Jesus.
···
On Tue, Nov 2, 2010 at 5:19 PM, Paul Roche <prpaulroche@gmail.com> wrote:
Hi. I'd like to print out the contents of 2 arrays as follows........
#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
···
On 02.11.2010 17:36, Rick DeNatale wrote:
On Tue, Nov 2, 2010 at 12:19 PM, Paul Roche<prpaulroche@gmail.com> wrote:
Hi. I'd like to print out the contents of 2 arrays as follows........