Skip to content

Instantly share code, notes, and snippets.

@fsvehla
Created February 12, 2009 12:12
Show Gist options
  • Save fsvehla/62595 to your computer and use it in GitHub Desktop.
Save fsvehla/62595 to your computer and use it in GitHub Desktop.
module DeepFreezing
# „Deep freezes“ the array, that is:
# - if the element responds to deep_freeze itself,
# like Array and Hash should, call it.
# - if the element responds to freeze, call it
# - freeze self
#
# ==== Returns
# self
#
# ==== Considerations
# Circular references are not resolved and might result in a deadlock,
# so don't do that.
def deep_freeze
self.each do |element|
if element.respond_to?(:deep_freeze)
element.deep_freeze
elsif element.respond_to?(:freeze)
element.freeze
end
end
self.freeze
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment