diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/duplicable.rb | 37 | ||||
-rw-r--r-- | activesupport/test/core_ext/duplicable_test.rb | 22 |
3 files changed, 61 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index fd0bd573c1..8d15d6622f 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* object.duplicable? returns true if object.dup is safe. False for nil, true, false, symbols, and numbers; true otherwise. #9333 [sur] + * Time, Date and DateTime #advance accept :weeks option. #9866 [Geoff Buesing] * Fix Time#years_ago and #years_since from leap days. #9865 [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/duplicable.rb b/activesupport/lib/active_support/core_ext/duplicable.rb new file mode 100644 index 0000000000..adbbfd8c60 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/duplicable.rb @@ -0,0 +1,37 @@ +class Object + # Can you safely .dup this object? + # False for nil, false, true, symbols, and numbers; true otherwise. + def duplicable? + true + end +end + +class NilClass #:nodoc: + def duplicable? + false + end +end + +class FalseClass #:nodoc: + def duplicable? + false + end +end + +class TrueClass #:nodoc: + def duplicable? + false + end +end + +class Symbol #:nodoc: + def duplicable? + false + end +end + +class Numeric #:nodoc: + def duplicable? + false + end +end diff --git a/activesupport/test/core_ext/duplicable_test.rb b/activesupport/test/core_ext/duplicable_test.rb new file mode 100644 index 0000000000..7f8c0f7d3b --- /dev/null +++ b/activesupport/test/core_ext/duplicable_test.rb @@ -0,0 +1,22 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class DuplicableTest < Test::Unit::TestCase + NO = [nil, false, true, :symbol, 1, 2.3, BigDecimal.new('4.56')] + YES = ['1', Object.new, /foo/, [], {}] + + def test_duplicable + NO.each do |v| + assert !v.duplicable? + begin + v.dup + fail + rescue Exception + end + end + + YES.each do |v| + assert v.duplicable? + assert_nothing_raised { v.dup } + end + end +end |