mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
object.duplicable? returns true if object.dup is safe. False for nil, true, false, symbols, and numbers; true otherwise. References #9333.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7906 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -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]
|
||||
|
||||
37
activesupport/lib/active_support/core_ext/duplicable.rb
Normal file
37
activesupport/lib/active_support/core_ext/duplicable.rb
Normal file
@@ -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
|
||||
22
activesupport/test/core_ext/duplicable_test.rb
Normal file
22
activesupport/test/core_ext/duplicable_test.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user