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
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
require 'enumerator' # if necessary
open(__FILE__) do |f|
f.each_slice(3) do |lines|
p lines
end
end
David
···
On Sat, 24 Oct 2009, Haoqi Haoqi wrote:
--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com
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
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: