[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Too many open files on OpenBSD

Yukihiro Matsumoto

3/6/2008 1:08:00 AM

Hi,

In message "Re: Too many open files on OpenBSD"
on Thu, 6 Mar 2008 09:32:58 +0900, Edd Barrett <vext01@gmail.com> writes:

|This is the error I am stuck with:
|
|---8<---
|./tlpsrcnode.rb:19:in `initialize': Too many open files -
|/home/tl/tl/Master/tlpkg/tlpsrc/wordcount.tlpsrc (Errno::EMFILE)
| from ./tlpsrcnode.rb:19:in `new'
| from ./tlpsrcnode.rb:19:in `parse'
| from ./tlpsrcnode.rb:30:in `parse'
| from ./tlpsrcnode.rb:20:in `each'
| from ./tlpsrcnode.rb:20:in `parse'
| from ./tlpsrcnode.rb:30:in `parse'
| from ./tlpsrcnode.rb:20:in `each'
| from ./tlpsrcnode.rb:20:in `parse'
| ... 3055 levels...
| from ./tlpsrcnode.rb:20:in `each'
| from ./tlpsrcnode.rb:20:in `parse'
| from ./roottlpsrcnode.rb:53:in `startParse'
| from ./rbmfsplit:54
|---8<---

It seems you recursively call parse, which keep files opened. How
about reading whole file content in a string by File.read, then call
each_line on the string? E.g.

def parse()
File.read(@path).each_line do |line|
line =~ /^(.*?) (.*)/

....
end
end

--
matz.

1 Answer

Damjan Rems

3/6/2008 7:24:00 AM

0

Yukihiro Matsumoto wrote:
> Hi,
>
> It seems you recursively call parse, which keep files opened. How
> about reading whole file content in a string by File.read, then call
> each_line on the string? E.g.
>
> def parse()
> File.read(@path).each_line do |line|
> line =~ /^(.*?) (.*)/
>
> ....
> end
> end

Or even better:

def parse()
lines = File.open(@path) {|f| f.read}
lines.each_line do |line|
line =~ /^(.*?) (.*)/

....
end
end

With File.open file gets closed automaticly after block is processed.

by
TheR
--
Posted via http://www.ruby-....