From: Anurag Priyam Date: 2011-01-14T07:12:03+09:00 Subject: Re: Retrieving and copying element from array > Thanks for reply, Anurag. Can't get this to work though: > > irb(main):001:0> lines = File.readlines('test.txt') > => ["category: cat1\n", " item1\n", " item2\n", " item3\n", "category: > cat2\n", " item1\n", "category: cat3\n", " item1\n", " item2\n", " > item3\n", " item4\n", "\n"] > irb(main):002:0> puts lines > category: cat1 >  item1 >  item2 >  item3 > category: cat2 >  item1 > category: cat3 >  item1 >  item2 >  item3 >  item4 > > => nil > irb(main):003:0> result = [] > => [] > irb(main):004:0> lines.each_slice(2) { |i, j| > result.push(i.sub(/category: /, '')); b.push(*j.split(', '))} > NameError: undefined local variable or method `b' for main:Object My bad; typed in wrong. The 'b' should be 'result' - we want to collect the processed element in the same array. >> result = [] >> lines.each_slice(2) {|i, j| result.push(i.sub(/category: /, '')); result.push(*j.split(', '))} -- Anurag Priyam http://about.me/yeban/