Last active
December 12, 2015 09:19
-
-
Save jimjh/4750671 to your computer and use it in GitHub Desktop.
deep_freeze ruby enumerables.
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
# ~*~ 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