From eb49fbc992aa1a8e0a4a6909e5f91e53fe0d332d Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@ruby-lang.org>
Date: Thu, 31 Oct 2013 14:07:09 +0900
Subject: [PATCH] weakref.rb: non-delegation

* lib/weakref.rb (WeakRef.[]): return non-delegation WeakRef object.
  [ruby-core:44402] [Feature #6308]
---
 lib/weakref.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/lib/weakref.rb b/lib/weakref.rb
index 36cbe3f..bbe8fbf 100644
--- a/lib/weakref.rb
+++ b/lib/weakref.rb
@@ -73,6 +73,55 @@ class WeakRef < Delegator
 
   @@__map = ::ObjectSpace::WeakMap.new
 
+  class << (@@__wrapper = Object.new) # :nodoc:
+    def inspect
+      obj = get
+      "#<WeakRef:#{obj ? obj.inspect : 'dead'}>"
+    end
+
+    def get
+      @@__map[self]
+    end
+
+    def weakref_alive?
+      !!@@__map[self]
+    end
+
+    freeze
+  end
+
+  @@__const_wrapper = Struct.new(:get) # :nodoc:
+  @@__const_wrapper.class_eval do
+    def weakref_alive?
+      true
+    end
+  end
+
+  def self.[](obj)
+    case obj
+    when true, false, nil, ::Symbol, ::Fixnum, ::Float
+      @@__const_wrapper.new(obj)
+    else
+      @@__map[w = @@__wrapper.clone] = obj
+      w
+    end
+  end
+
+  def self.new(*)
+    if $VERBOSE
+      label = caller_locations(1, 1)[0].to_s.freeze
+      (@__warned ||= {})[label] ||=
+        begin
+          warn <<-MSG
+#{label}: WeakRef.new with delegation is deprecated, use WeakRef[]
+#{label}: instead and use `get' method to achieve the target object.
+          MSG
+          true
+        end
+    end
+    super
+  end
+
   ##
   # Creates a weak reference to +orig+
   #
-- 
1.8.3.4

