Skip to content

Instantly share code, notes, and snippets.

@nahi
Created June 21, 2011 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahi/1037082 to your computer and use it in GitHub Desktop.
Save nahi/1037082 to your computer and use it in GitHub Desktop.
Patch for ruby_1_8_7 singleton.rb
Index: lib/singleton.rb
===================================================================
--- lib/singleton.rb (revision 32126)
+++ lib/singleton.rb (working copy)
@@ -94,9 +94,12 @@
@__instance__ = new
ensure
if @__instance__
- class <<self
- remove_method :instance
- def instance; @__instance__ end
+ # Check method existence to reduce redefinition warning.
+ unless self.respond_to?(:instance)
+ # It's doing test-then-set without a lock so there's a race to
+ # override existing method that causes the redefinition warning.
+ # We allow it since method redefinition is thread-safe for calling.
+ def self.instance; @__instance__ end
end
else
@__instance__ = nil # failed instance creation
@@ -111,9 +114,9 @@
@__instance__ = new
ensure
if @__instance__
- class <<self
- remove_method :instance
- def instance; @__instance__ end
+ # See the comment for self.instance above.
+ unless self.respond_to?(:instance)
+ def self.instance; @__instance__ end
end
else
@__instance__ = nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment