gitmyhub

ref

Ruby ★ 64 updated 5y ago ▣ archived

A weak reference implementation for Ruby that works across runtimes (MRI, REE, Jruby, Rubinius, and IronRuby)

A Ruby library that provides weak, soft, and strong references plus ready-made caches, letting your program hold onto objects without running out of memory.

RubyMRIJRubyRubiniussetup: easycomplexity 2/5

Ref is a Ruby library that lets you hold onto objects without forcing them to stay in memory forever. The core idea is "weak references" — pointers to data that the garbage collector can clean up whenever it needs free memory. This matters when you're caching things or building data structures where you'd like to keep stuff around for reuse but don't want to risk your program consuming ever-growing amounts of RAM.

Ruby gives you three flavors of references here. A strong reference is just a normal pointer — the object stays alive as long as something points to it. A weak reference lets the garbage collector reclaim the object at any time, so you can check if it's still there and use it if so, or recreate it if not. A soft reference sits in between: the collector can reclaim it, but it's less aggressive about doing so. The library also includes ready-made data structures like weak-key and weak-value maps, plus a thread-safe queue that tracks when objects get collected.

This is useful for developers building caching layers, memoization, or any system where you store computed results for potential reuse. For example, if you're building a web app that caches rendered pages, weak references let you keep recent pages handy without the cache growing uncontrollably. The garbage collector handles eviction for you — when memory pressure rises, it reclaims what it can.

What makes this project notable is that it solves a cross-platform problem. Ruby's built-in WeakRef class has bugs on some runtimes — particularly on YARV 1.9, where a race condition could cause a reference to point to the wrong object entirely. This gem provides a consistent, safe interface that works across MRI, JRuby, Rubinius, and other Ruby implementations, so developers don't have to worry about which runtime their code will run on.

Where it fits