Table of contents


xchan.rb is a small and easy to use library for sharing Ruby objects between Ruby processes who have a parent-child relationship.



The first example introduces you to the xchan method, it is implemented as Object#xchan and returns an instance of XChan::UNIXSocket.

The first (optional) argument to xchan is an object who can dump an object to text and from that text create the same object once again in memory. xchan.rb defaults to Marshal without an argument, depending on your needs you could choose from YAML, JSON, and MessagePack - to name a few.

require 'xchan'
ch = xchan
Process.wait fork {
  ch.send "Hi parent"
  ch.send "Bye parent"
puts ch.recv
puts ch.recv


The following example demonstrates how to send and receive objects within a 0.5 second timeout, using the #timed_send and #timed_recv methods. nil is returned when either method times out.

require 'xchan'
ch = xchan
ch.timed_send("Hello parent", 0.5) ? puts("message sent") : puts("send timed out")
(message = ch.timed_recv 0.5) ? puts(message) : puts("read timed out")


The following example demonstrates the #recv_last method, it reads the last object written to a channel and discards older writes in the process ("ab" and "abc" in this example).

require 'xchan'
ch = xchan
ch.send "ab"
ch.send "abc"
ch.send "abcd"
puts ch.recv_last # => "abcd"

examples/ directory

The examples/ directory contains the above examples, try them by running:

ruby -Ilib examples/example_X.rb


Documentation coverage for xchan.rb is at over 60%, and as of this writing all methods are documented. The documentation is written with YARD.


gem install xchan.rb


The MIT license, see LICENSE.txt for details.