Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created September 2, 2009 16:05
Show Gist options
  • Save brianjlandau/179791 to your computer and use it in GitHub Desktop.
Save brianjlandau/179791 to your computer and use it in GitHub Desktop.
Deep freeze for enumerable objects in Ruby
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