Created
September 2, 2009 16:05
-
-
Save brianjlandau/179791 to your computer and use it in GitHub Desktop.
Deep freeze for enumerable objects in Ruby
This file contains 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
module Enumerable | |
def deep_freeze | |
unless self.is_a? String | |
frozen = self.dup.each do |key, value| | |
if (value.is_a?(Enumerable) && !value.is_a?(String)) | |
value.deep_freeze | |
else | |
value.freeze | |
end | |
end | |
self.replace(frozen) | |
end | |
self.freeze | |
end | |
end | |
class Hash | |
def deep_freeze | |
frozen = self.dup.each do |key, value| | |
if (value.is_a?(Enumerable) && !value.is_a?(String)) | |
value.deep_freeze | |
else | |
value.freeze | |
end | |
end | |
self.replace(frozen) | |
self.freeze | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment