# Can you implement a better IO#each\_lines?

**URL:** <https://www.ruby-forum.com/t/can-you-implement-a-better-io-each-lines/177814>\
**Category:** Ruby\
**Created:** [October 24, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/can-you-implement-a-better-io-each-lines/177814 "2009-10-24T13:24:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 24, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/can-you-implement-a-better-io-each-lines/177814/1 "2009-10-24T13:24:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 24, 2009, 2:09pm UTC](https://www.ruby-forum.com/t/can-you-implement-a-better-io-each-lines/177814/2 "2009-10-24T14:09:27Z")

</div>

Hi –

On Sat, 24 Oct 2009, Haoqi H. wrote:

> ```
> run=false
> 
> ```
> 
> open( **FILE** ) do |f|  
> f.each\_lines(3) do |lines|  
> p lines  
> end  
> end

require ‘enumerator’ # if necessary  
open( **FILE** ) do |f|  
f.each\_slice(3) do |lines|  
p lines  
end  
end

🙂

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 24, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/can-you-implement-a-better-io-each-lines/177814/3 "2009-10-24T14:15:54Z")

</div>

On 10/24/2009 01:24 PM, Haoqi H. wrote:

> ```
> run=false
> 
> ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 24, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/can-you-implement-a-better-io-each-lines/177814/4 "2009-10-24T14:40:54Z")

</div>

great~thank you!

---

<div class="post-metadata">

**Author:** ![haoqi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ed655f/32.png) [@haoqi](https://www.ruby-forum.com/u/haoqi)\
**Post date:** [October 24, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/can-you-implement-a-better-io-each-lines/177814/5 "2009-10-24T14:19:24Z")

</div>

On Sat, 24 Oct 2009, Robert K. wrote:

> > ```
> > unless ln
> > 
> > ```
> 
> [“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.\*.

And 1.8.6 if you require ‘enumerator’.

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
