Created
May 20, 2010 16:10
-
-
Save jcoglan/407741 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def deep_copy(object) | |
| case object | |
| when Array | |
| object.map { |item| deep_copy(item) } | |
| when Hash | |
| object.inject({}) do |hash, (key,value)| | |
| hash[deep_copy(key)] = deep_copy(value) | |
| hash | |
| end | |
| # handle other data structures if need be | |
| else | |
| object.respond_to?(:dup) ? object.dup : object | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The slower, but simpler approach: