Permalink
Browse files

BenString.in_use? to find out if BenString is in use.

  • Loading branch information...
1 parent 0ee90c3 commit 244aa8e8086fb7f30c0ffc463d71e8e2aa298457 @zerowidth zerowidth committed Nov 17, 2010
Showing with 23 additions and 0 deletions.
  1. +12 −0 lib/ben_string.rb
  2. +11 −0 test/test_ben_string.rb
View
@@ -1,6 +1,18 @@
class BenString < String
VERSION = '1.1.0'
+ class << self
+
+ public
+
+ def in_use?
+ counts = Hash.new(0)
+ ObjectSpace.each_object { |o| counts[o.class] += 1 }
+ counts[self] > 0
+ end
+
+ end
+
public
def to_bool()
View
@@ -11,4 +11,15 @@ def test_to_ben_s
str = BenString.new
assert_equal str, str.to_ben_s
end
+
+ def test_not_in_use
+ GC.start
+ assert !BenString.in_use?
+ end
+
+ def test_in_use
+ str = BenString.new
+ assert BenString.in_use?
+ end
+
end

0 comments on commit 244aa8e

Please sign in to comment.