On Fri, Aug 01, 2003 at 09:30:19PM +0900, dblack / superlink.net wrote:
> If you just want the count of lines with plastic_1.1..., you could do:
> 
>   plastic_count = file.readlines.grep(/plastic_1.1.../).size
> 
> or, to save reading the whole file in at once:
> 
>   plastic_count = 0
>   file.readlines.each do |line|
>     plastic_count += 1 if /plastic_1.1.../.match(line)
>   end

That example also reads in the entire file: that's what 'readlines' does :-)

But File is an Enumerable object. Hence you can do

    plastic_count = file.grep(/plastic_1.1.../).size

(which will generating an intermediate array of matching lines only), or

    plastic_count = 0
    file.each do |line|
      plastic_count += 1 if /plastic_1.1.../.match(line)
    end

(which generates no intermediate array at all)

Regards,

Brian.