From 56ccc7c73949e92b6c942a29076653e8a3e5eb55 Mon Sep 17 00:00:00 2001 From: Lourens Naude Date: Wed, 28 Jan 2009 17:10:24 +0000 Subject: [PATCH] Introduce Object#dup? to accommodate special case constants ( TrueClass, Fixnum etc. ) that do respond to #dup, but raises TypeError when invoked. --- .../active_support/cache/strategy/local_cache.rb | 2 +- .../lib/active_support/core_ext/object/misc.rb | 11 ++++++++++- .../test/core_ext/object_and_class_ext_test.rb | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 621358d..761ab4d 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -41,7 +41,7 @@ module ActiveSupport value else # forcing the value to be immutable - value.dup + value.dup? ? value.dup : value end end diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index 4acdfa3..b8ff6bb 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -1,4 +1,7 @@ -class Object +class Object + + SPECIAL_CONSTANTS = [ TrueClass, FalseClass, NilClass, Symbol, Fixnum, BigDecimal, Float ] + # Returns +value+ after yielding +value+ to the block. This simplifies the # process of constructing an object, performing work on the object, and then # returning the object from a method. It is a Ruby-ized realization of the K @@ -87,4 +90,10 @@ class Object respond_to? "acts_like_#{duck}?" end + # Identify if this object can be duped.Instances of NilClass, TrueClass, FalseClass + # and Fixnum do respond to #dup, but raises a TypeError when called. + def dup? + !SPECIAL_CONSTANTS.include?( self.class ) + end + end diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index 0bdbd14..3d97d68 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -280,3 +280,18 @@ class ObjectTryTest < Test::Unit::TestCase assert_equal 'false', false.try(:to_s) end end + +class ObjectDupTest < Test::Unit::TestCase + + def test_immuteable_objects_should_not_be_duped + assert !nil.dup? + assert !true.dup? + assert !false.dup? + assert !1.334.dup? + assert 12.dup? + assert !:symbol.dup? + assert 'string'.dup? + assert Class.new.dup? + end + +end -- 1.5.4.5 From 7e3706b8650b7fc8fcba7d1b67f8395c6b347653 Mon Sep 17 00:00:00 2001 From: Lourens Naude Date: Wed, 28 Jan 2009 17:33:23 +0000 Subject: [PATCH] Alias Object#clone? to Object#dup? --- .../lib/active_support/core_ext/object/misc.rb | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index b8ff6bb..4335ef8 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -95,5 +95,6 @@ class Object def dup? !SPECIAL_CONSTANTS.include?( self.class ) end + alias :clone? :dup? end -- 1.5.4.5