Take the 2-minute tour ×

Does Ruby offer a mechanism to share variables (more importantly, class objects and any other data abstractions I deem useful for that matter) between different running Ruby processes?

For example if I have a class instantiated, initialized and carefully tuned to a certain state, I want that state to sort of globally be available to all my otherwise independent Ruby and Irb runs throughout the day, outliving the lifetime of the process that initially used it.

One scenario I'm now considering to effectively address this problem is to create a micro Ruby script acting merely as a keeper of my chosen persistent objects and variables for a day, and daemonize it with the daemons gem or similar. Set-up 1st in the morning, tear-down at night at the end of all things.

Throughout the day many instances of another Ruby program--the one containing my actual business logic--would come and go yet be able to use and manipulate my chosen objects kept alive in the daemon process above.

Should I be studying how to serialize complex objects to disk? (actually prefer RAM not disk since it's temporary for a period anyway, and recreating it only costs much time)

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You're probably looking for something like memcache (Gem) or redis (Gem).

You may also be interested in redis-objects.

share|improve this answer
memcache-client replaced by dalli –  Marcos Jan 12 '12 at 17:31

The maglev implementation of Ruby seems to be perfect for your needs as it provides persistent distributed Ruby objects.

The MagLev VM takes full advantage of GemStone/S JIT to native code performance, distributed shared cache, fully ACID transactions, and enterprise class NoSQL data management capabilities to provide a robust and durable programming platform. It can transparently manage a much larger amount (terabytes) of data and code than will fit in memory. There are no restrictions on what types of objects, classes, blocks, threads or continuations that can be stored and executed.

But it might be a bit overkill depending on the size of your project.

share|improve this answer
Thanks. The main object I'm trying to persist is the instance driver from watir-webdriver gem, linking to a huge memory hog that is a Firefox browser process, which takes too long to load and unload every time my script runs. $browser = Watir::Browser.new(:firefox) opens a new browser but sadly, the class offers no built-in ability to detach and re-attach to prior web browser sessions left behind by a previous irb or ruby Watir::Browser instance. Which is why I'm instead looking for a way to keep that object instance itself alive after Ruby exits. –  Marcos Dec 28 '11 at 2:15
Maybe you actually want to write a program that never exists and send it requests over TCP or UNIX sockets, rather than command line arguments? –  d11wtq Dec 28 '11 at 3:26
@d11wtq Not sure how that could fit in to accessing $browser's vast array of methods and elements, and its nodes and subnodes and sub sub... and their methods as well, many returning or needing other Watir type objects. –  Marcos Dec 28 '11 at 10:03

Your Answer


