Skip to content

Instantly share code, notes, and snippets.

@jimjh
Last active December 12, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimjh/4750671 to your computer and use it in GitHub Desktop.
Save jimjh/4750671 to your computer and use it in GitHub Desktop.
deep_freeze ruby enumerables.
# ~*~ encoding: utf-8 ~*~
module Enumerable
# Freezes elements and nested containers recursively.
def deep_freeze
each { |o| o.respond_to?(:deep_freeze) ? o.deep_freeze : o.freeze }
freeze
end
end
# == Usage
# x = {a: {b: 'c'}}.deep_freeze
# x[:a][:b].upcase! # >> RuntimeError: can't modify frozen String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment