# Bi-directional pipe?

**URL:** <https://www.ruby-forum.com/t/bi-directional-pipe/88723>\
**Category:** Ruby\
**Created:** [March 13, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/bi-directional-pipe/88723 "2007-03-13T21:15:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 13, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/bi-directional-pipe/88723/1 "2007-03-13T21:15:43Z")

</div>

Is there any standard Ruby way of creating a bidirectional pipe or  
socketpair?

I want to pass a single IO-like object around, but to be able to both  
read  
and write from the other end. (What I’m actually trying to do is to put  
a  
facade around Net::SSH so that the command channel can be passed in as a  
proxy to Net::Telnet, but I can think of other uses of this)

IO.popen(…, “w+”) does make such a bidirectional pipe, but as far as I  
can  
see only for communicating with a child process.

Any ideas?

Thanks,

Brian.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 13, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/bi-directional-pipe/88723/2 "2007-03-13T21:29:46Z")

</div>

On Mar 13, 2007, at 4:15 PM, Brian C. wrote:

> IO.popen(…, “w+”) does make such a bidirectional pipe, but as far  
> as I can  
> see only for communicating with a child process.
> 
> Any ideas?
> 
> Thanks,
> 
> Brian.

rd, wr = IO.pipe

comes to mind.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 13, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/bi-directional-pipe/88723/3 "2007-03-13T21:36:46Z")

</div>

On Wed, 14 Mar 2007, Rob B. wrote:

> On Mar 13, 2007, at 4:15 PM, Brian C. wrote:
> 
> > Is there any standard Ruby way of creating a bidirectional pipe or  
> > socketpair?

require ‘socket’  
Socket.pair …

-a

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 13, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/bi-directional-pipe/88723/4 "2007-03-13T22:00:49Z")

</div>

On Wed, Mar 14, 2007 at 05:29:16AM +0900, Rob B. wrote:

> > proxy to Net::Telnet, but I can think of other uses of this)
> 
> rd, wr = IO.pipe
> 
> comes to mind.

That’s a _unidirectional_ pipe. At least, all the rdoc stuff says that  
one  
end is a reader and the other end is a writer.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 13, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/bi-directional-pipe/88723/5 "2007-03-13T22:02:49Z")

</div>

On Wed, Mar 14, 2007 at 05:35:52AM +0900, removed\_email\_address@domain.invalid wrote:

> On Wed, 14 Mar 2007, Rob B. wrote:
> 
> > On Mar 13, 2007, at 4:15 PM, Brian C. wrote:
> > 
> > > Is there any standard Ruby way of creating a bidirectional pipe or  
> > > socketpair?
> 
> require ‘socket’  
> Socket.pair …

irb(main):015:0\> require ‘socket’  
=\> true  
irb(main):016:0\> a = Socket.pair  
ArgumentError: wrong number of arguments (0 for 3)  
from (irb):16:in `pair’  
from (irb):16  
from :0

OK, so it exists, it’s just not documented in ri - time to grep the  
source I  
guess. Thanks for the pointer.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [March 13, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/bi-directional-pipe/88723/6 "2007-03-13T22:17:51Z")

</div>

On Wed, 14 Mar 2007, Brian C. wrote:

> irb(main):015:0\> require ‘socket’  
> =\> true  
> irb(main):016:0\> a = Socket.pair  
> ArgumentError: wrong number of arguments (0 for 3)  
> from (irb):16:in `pair’  
> from (irb):16  
> from :0
> 
> OK, so it exists, it’s just not documented in ri - time to grep the source I  
> guess. Thanks for the pointer.

[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/26d3538da0410b8b/fdc3c5c6686ceb49?lnk=gst&q=Socket.pair&rnum=2&hl=en#fdc3c5c6686ceb49](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/26d3538da0410b8b/fdc3c5c6686ceb49?lnk=gst&q=Socket.pair&rnum=2&hl=en#fdc3c5c6686ceb49)

[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/735d4f73074db10f/5a38eadb8db2d832?lnk=gst&q=Socket.pair&rnum=3&hl=en#5a38eadb8db2d832](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/735d4f73074db10f/5a38eadb8db2d832?lnk=gst&q=Socket.pair&rnum=3&hl=en#5a38eadb8db2d832)

regards.

-a
