Bug #14031
closedWeakRef example misleading and wrong
Description
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
https://ruby-doc.org/stdlib-2.4.0/libdoc/weakref/rdoc/WeakRef.html
The example shows
GC.start
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 WeakHash.new('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) about 7 years ago
Semi-off topic - a variable named "omg" is for the win ... ;)
Updated by marcandre (Marc-Andre Lafortune) about 7 years ago
- Status changed from Open to Closed