Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Object#freeze

obj.freeze → obj

Prevents further modifications to obj. A RuntimeError will be raised if modification is attempted. There is no way to unfreeze a frozen object. See also Object#frozen?.

This method returns self.

Example
a = [ "a", "b", "c" ]
a.freeze
a << "z"

produces:

prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
 from prog.rb:3

Objects of the following classes are always frozen: Integer, Float, Symbol.

See also #extend #hash #eql? #inspect also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1