Skip to content

Instantly share code, notes, and snippets.

@kyletolle
Created October 24, 2013 20:34
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 kyletolle/7144522 to your computer and use it in GitHub Desktop.
Save kyletolle/7144522 to your computer and use it in GitHub Desktop.
hash freezing example
irb(main):007:0> a
=> #<Object:0x007f892b8e6208>
irb(main):008:0> a = Object.new
=> #<Object:0x007f892b93a1f0>
irb(main):009:0> b = Object.new
=> #<Object:0x007f892b940af0>
irb(main):010:0> c = Object.new
=> #<Object:0x007f89291125d8>
irb(main):011:0> h = { a: a, b: b, c: c }
=> {:a=>#<Object:0x007f892b93a1f0>, :b=>#<Object:0x007f892b940af0>, :c=>#<Object:0x007f89291125d8>}
irb(main):013:0> h.freeze
=> {:a=>#<Object:0x007f892b93a1f0>, :b=>#<Object:0x007f892b940af0>, :c=>#<Object:0x007f89291125d8>}
irb(main):014:0> h[:a] = Object.new
RuntimeError: can't modify frozen Hash
from (irb):14:in `[]='
from (irb):14
from /Users/kyle/.rubies/ruby-2.0.0-p247/bin/irb:12:in `<main>'
irb(main):015:0> a = Object.new
=> #<Object:0x007f892b9a3b00>
irb(main):016:0> h
=> {:a=>#<Object:0x007f892b93a1f0>, :b=>#<Object:0x007f892b940af0>, :c=>#<Object:0x007f89291125d8>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment