What is dRuby?

dRuby (DRb) lets one Ruby process call methods on objects that live in another. What it does, how a call travels, and when it fits.

Updated

dRuby, also written DRb, is Ruby's distributed object library. It lets one Ruby process call methods on an object that lives in another Ruby process, on the same machine or across a network, using ordinary method call syntax. It was written by Masatoshi Seki and is itself written entirely in Ruby.

The idea in one example

A server process publishes one object, called the front object, at a URI. A client process makes a stand-in for that object and calls methods on it.

# In the client
require 'drb/drb'

DRb.start_service
clock = DRbObject.new_with_uri('druby://localhost:8787')
puts clock.now

clock is not the real object. It is a DRb::DRbObject, a small proxy that knows the server's URI. When you call now on it, dRuby sends the method name and arguments to the server, the real object runs the method there, and the result comes back as the return value. The full pair of scripts is on a first server and client.

How a call travels

  1. The client calls a method on the proxy.
  2. dRuby serializes the method name and arguments with Ruby's Marshal and sends them over a socket.
  3. The server finds the target object, calls the method, and marshals the result.
  4. The client unmarshals the result and returns it. If the method raised an exception on the server, the exception is raised in the client.

Because Marshal is used, most arguments and return values arrive as copies. Objects that cannot or should not be copied travel as references instead. That difference is the most important thing to understand about dRuby, and it has its own page: pass by value and pass by reference.

There is no fixed client or server

Every dRuby process can both make calls and receive them. A "client" that passes a block or a callback object to a "server" is, for that moment, serving requests itself. That is why even clients usually call DRb.start_service; see callbacks and blocks.

What it is good for

  • Letting a handful of Ruby processes that you control cooperate: workers, a shared counter, a job queue, a console attached to a running program.
  • Prototyping a distributed design quickly, with no interface definitions and no code generation.
  • Coordinating processes through a shared tuple space with Rinda.

What it is not

  • It is Ruby to Ruby. Both ends speak Ruby's marshal format, so it is not a way to talk to programs in other languages.
  • It is not a public API protocol. A dRuby port gives a caller a great deal of power over the server process, so it belongs on localhost or a trusted private network only. Read why dRuby must not face untrusted networks before you bind to anything else.

Where it comes from

dRuby shipped with Ruby as part of the standard library for many years. From Ruby 3.4 it is a bundled gem, which changes how you declare it in a Gemfile. See installing dRuby.