# Problems with IO.popen in ruby 1.8.2?

**URL:** <https://www.ruby-forum.com/t/problems-with-io-popen-in-ruby-1-8-2/106036>\
**Category:** Ruby\
**Created:** [June 5, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/problems-with-io-popen-in-ruby-1-8-2/106036 "2007-06-05T21:03:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [June 5, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/problems-with-io-popen-in-ruby-1-8-2/106036/1 "2007-06-05T21:03:38Z")

</div>

I have the following code snippet:

```
sleep 0.3
IO.popen('/usr/bin/xclip -o').readlines {|f| print}

```

running in a tight loop. If I let it run for a short time (about 25  
seconds), I get this error:

```
Enter URL: ./dice.rb:89:in `popen': Too many open files - 

```

/usr/bin/xclip -o (Errno::EMFILE)  
from ./dice.rb:89:in `read' from ./dice.rb:181 from ./dice.rb:162:in`loop’  
from ./dice.rb:162

I don’t understand the nature of the error, since the pipe is implicitly  
closed when the block exits, right? Event if I change the line to:

```
IO.popen('/usr/bin/xclip -o').readlines {|f| print; f.close}

```

I still have the same problem. So, what file resource is being consumed,  
and how do I fix it?

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [June 5, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/problems-with-io-popen-in-ruby-1-8-2/106036/2 "2007-06-05T22:06:02Z")

</div>

On Jun 5, 2007, at 1:02 PM, Todd A. Jacobs wrote:

> ```
> from ./dice.rb:89:in `read'
> 
> ```
> 
> I still have the same problem. So, what file resource is being  
> consumed,  
> and how do I fix it?

read carefully - you’ve given the block to readlines - not popen.  
invert your logic

IO.popen(cmd) do |pipe|  
pipe.readlines{|line| print line}  
end

> –  
> “Oh, look: rocks!”  
> – Doctor Who, “Destiny of the Daleks”

-a
