On 10/24/2009 01:24 PM, Haoqi Haoqi wrote:
> like this,thanks
> #!/usr/bin/env ruby
> class IO
>   def each_lines(n)
>     run=true
>     while run
>       lines=[]
>       n.times{
>          ln = self.gets
>          unless ln
>           run=false
>           break
>           else
>             lines<<ln.chomp
>         end
>       }
>       yield lines unless lines.empty?
>     end
>   end
> end
> 
> open(__FILE__) do |f|
>    f.each_lines(3) do |lines|
>      p lines
>    end
> end

There is no need to implement anything.  You can use #each_slice:

robert@fussel:~$ seq 1 10 | ruby1.9 -e '$stdin.each_slice(3) {|l| p l}'
["1\n", "2\n", "3\n"]
["4\n", "5\n", "6\n"]
["7\n", "8\n", "9\n"]
["10\n"]
robert@fussel:~$

This works in 1.8.7 and 1.9.*.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/