From: Simon Harrison Date: 2011-01-14T05:19:47+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 from (irb):4:in `block in irb_binding' from (irb):4:in `each' from (irb):4:in `each_slice' from (irb):4 from /usr/local/bin/irb:12:in `
' irb(main):005:0> lines.each_slice(2) { |i, j| result.push(i.sub(/category: /, '')); j.push(*j.split(', '))} NoMethodError: undefined method `push' for " item1\n":String from (irb):5:in `block in irb_binding' from (irb):5:in `each' from (irb):5:in `each_slice' from (irb):5 from /usr/local/bin/irb:12:in `
' -- Posted via http://www.ruby-forum.com/.