On Sat, Jan 15, 2011 at 8:10 AM, Simon H.
[email protected]wrote:
That works great, thanks Josh. A couple of questions if you don’t mind.
- What is the purpose of the [] in line below? Does it mean collect
whatever matches into an array?
categories << [ line.sub(/^category: /,‘’).chomp ]
Yes, but not whatever matches. The call to #sub, with the second arg
being
an empty string, says to remove "category: " from the string, if it is
at
the beginning. And the chomp removes the newline. So if line is
“category:
cat1\n”, then line.sub(/^category: /,‘’).chomp will return “cat1”. Then
we
stick that in the Array
irb(main):041:1* if item =~ /^cat/
irb(main):042:2> arr2 << [ item ]
irb(main):043:2> else
irb(main):044:2* arr2.last << item
irb(main):045:2> end
irb(main):046:1> end
You are right on, here, just getting confused about your data format,
again.
Your code will work correctly if arr is an array of the lines of your
file,
such as you would get with File.readlines.
In other words, in your irb example,
arr is [[“cat1”, “1”, “2”, “3”], [“cat2”, “1”, “2”], [“cat3”, “1”, “2”]]
but in mine, it was read in straight from the file, so it would be
[“cat1”, “1”, “2”, “3”, “cat2”, “1”, “2”, “cat3”, “1”, “2”]
If you fix that, it will work correctly.
As a side note, you are doing arr.map (
module Enumerable - RDoc Documentation), but what you
really mean is arr.each
(class Array - RDoc Documentation).
It isn’t harming anything, but it is misleading, because map implies you
are
trying to create a new array by collecting the results of the blocks for
each element, but really you are just trying to iterate.