diff options
author | Emilio Tagua <miloops@gmail.com> | 2010-10-14 13:35:05 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2010-11-19 19:08:28 -0300 |
commit | 0873d1e944147b0f4b15e742fa9df0776a024d51 (patch) | |
tree | 3d409f13324c37044b652ca066f3dd21ea450b74 /activesupport | |
parent | 86699339bc01f4dd8eefe86d04317b2e0b3cf233 (diff) | |
download | rails-0873d1e944147b0f4b15e742fa9df0776a024d51.tar.gz rails-0873d1e944147b0f4b15e742fa9df0776a024d51.tar.bz2 rails-0873d1e944147b0f4b15e742fa9df0776a024d51.zip |
Use conditional to avoid warnings.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/weak_hash.rb | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/activesupport/lib/active_support/weak_hash.rb b/activesupport/lib/active_support/weak_hash.rb index c6bf098074..171e38f9d8 100644 --- a/activesupport/lib/active_support/weak_hash.rb +++ b/activesupport/lib/active_support/weak_hash.rb @@ -1,46 +1,48 @@ module ActiveSupport - class WeakHash - def initialize(cache = Hash.new) - @cache = cache - @key_map = {} - @rev_cache = Hash.new{|h,k| h[k] = {}} - @reclaim_value = lambda do |value_id| - if value = @rev_cache.delete(value_id) - value.each_key{|key| @cache.delete key} + if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' + WeakHash = ::Weakling::WeakHash + else + class WeakHash + def initialize(cache = Hash.new) + @cache = cache + @key_map = {} + @rev_cache = Hash.new{|h,k| h[k] = {}} + @reclaim_value = lambda do |value_id| + if value = @rev_cache.delete(value_id) + value.each_key{|key| @cache.delete key} + end end end - end - def [](key) - value_id = @cache[key] - value_id && ObjectSpace._id2ref(value_id) - rescue RangeError - nil - end + def [](key) + value_id = @cache[key] + value_id && ObjectSpace._id2ref(value_id) + rescue RangeError + nil + end - def []=(key, value) - key2 = case key - when Fixnum, Symbol, true, false, nil - key - else - key.dup - end + def []=(key, value) + key2 = case key + when Fixnum, Symbol, true, false, nil + key + else + key.dup + end - @rev_cache[value.object_id][key2] = true - @cache[key2] = value.object_id - @key_map[key.object_id] = key2 + @rev_cache[value.object_id][key2] = true + @cache[key2] = value.object_id + @key_map[key.object_id] = key2 - ObjectSpace.define_finalizer(value, @reclaim_value) - end + ObjectSpace.define_finalizer(value, @reclaim_value) + end - def clear - @cache.clear - end + def clear + @cache.clear + end - def delete(key) - @cache.delete(key) + def delete(key) + @cache.delete(key) + end end end end - -ActiveSupport::WeakHash = ::Weakling::WeakHash if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' |