Why would you use freeze on a Ruby object?

Last updated 27 June 2026

freeze makes an object immutable: any attempt to modify it raises FrozenError. You use it to protect shared state, declare true constants, and gain small performance/memory wins.

  • Enforce immutability: Constants in Ruby aren't truly immutable; CONFIG = {...}.freeze prevents accidental in-place changes.

  • Safe sharing: A frozen object can be shared across threads/objects without fear of one party mutating it underneath another.

  • Performance / memory: Frozen strings can be deduplicated and used as efficient hash keys.

  • Caveat: shallow freeze: freeze only freezes the object itself, not nested objects. A frozen array can still have mutable elements; deep-freeze each element if needed.

[lang:ruby] config = { host: "localhost" }.freeze config[:host] = "other" # raises FrozenError arr = ["a"].freeze arr << "b" # FrozenError arr[0] << "x" # works! element isn't frozen

  • https://www.it-jobs-nl.com/ruby-developer/
  • https://www.bacancytechnology.com/blog/outsource-ruby-on-rails-development
  • https://blog.nextideatech.com/how-to-hire-the-best-ruby-on-rails-developers-the-complete-guide/
  • https://ubiminds.com/en-us/ruby-on-rails-developers/
  • https://dataford.io/interview-guides/pattern/software-engineer
All Ruby interview questions