lib/core/facets/kernel/deepcopy.rb in facets-2.1.3 vs lib/core/facets/kernel/deepcopy.rb in facets-2.2.0
- old
+ new
@@ -1,21 +1,5 @@
-# TITLE:
-#
-# Copy Extensions
-#
-# FILE:
-#
-# kernel/copy.rb
-#
-# DESCRIPTION:
-#
-# Kernel extensions for copying objects.
-#
-# AUTHORS:
-#
-# CREDIT Jan Molic
-
module Kernel
# Anything that can be marshaled can be copied in totality.
# This is also commonly called a deep_copy.
#
@@ -23,11 +7,11 @@
#
def copy
Marshal::load(Marshal::dump(self))
end
- alias deep_copy copy
+ alias_method :deep_copy, :copy
# Adds deep_clone method to an object which produces deep copy of it. It means
# if you clone a Hash, every nested items and their nested items will be cloned.
# Moreover deep_clone checks if the object is already cloned to prevent endless recursion.
#
@@ -35,10 +19,12 @@
# a = [ true, false, obj ]
# b = a.deep_clone
# obj.push( 'foo' )
# p obj # >> [ 'foo' ]
# p b[2] # >> []
+ #
+ # CREDIT: Jan Molic
def deep_clone( obj=self, cloned={} )
if cloned.has_key?( obj.object_id )
return cloned[obj.object_id]
else
@@ -69,43 +55,5 @@
end
end
end
end
-
-
-# _____ _
-# |_ _|__ ___| |_
-# | |/ _ \/ __| __|
-# | | __/\__ \ |_
-# |_|\___||___/\__|
-#
-=begin test
-
- require 'test/unit'
-
- class TestKernelCopy < Test::Unit::TestCase
-
- # fixtures for copy / deep_copy
- class A
- attr_reader :a
- def initialize
- @a = 1
- end
- end
-
- class B
- attr_reader :b
- def initialize
- @b = A.new
- end
- end
-
- def test_deep_copy
- o = B.new
- oc = o.deep_copy
- assert_equal( 1, o.b.a )
- end
-
- end
-
-=end