Issue #14031 has been reported by marcandre (Marc-Andre Lafortune).

----------------------------------------
Bug #14031: WeakRef example misleading and wrong
https://bugs.ruby-lang.org/issues/14031

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 2.5
* ruby -v: trunk
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
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
~~~ ruby
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.




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>