Bug #14031

WeakRef example misleading and wrong

Added by marcandre (Marc-Andre Lafortune) almost 3 years ago. Updated almost 3 years ago.

Target version:
ruby -v:


I just noticed that the second part of the doc of WeakRef is misleading and later plainly wrong.

I'm talking about the example with WeakHash in

The example shows

c['foo'] #=> nil
c['baz'] #=> nil
c['qux'] #=> nil

This is very misleading, since even before the GC that would be also the case, because WeakHash didn't redefine the lookup, and that'foo').eql?('foo') is always false.

The doc goes on with:
"You can see the local variable omg stayed, although its reference in our hash object was garbage collected, along with the rest of the keys and values."

That is wrong. The reference in our hash object was not garbage collected, since omg held on to it. This can be proven with c.values.last # => 'lol'.

My opinion is that fixing this example isn't worth it, and that even fixed it wouldn't add anything to the first simple example on WeakRef. In it's current form, it is worse dans not having it. Unless there are objections, I'll simply remove it.

Updated by shevegen (Robert A. Heiler) almost 3 years ago

Semi-off topic - a variable named "omg" is for the win ... ;)


Updated by marcandre (Marc-Andre Lafortune) almost 3 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r60404.

lib/weakref: Remove incorrect example [DOC] [Bug #14031]

Also available in: Atom PDF