Method: Object#freeze

Defined in:
object.c

#freezeObject

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.

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

produces:

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

Returns:


# File 'object.c'

/*
 *  call-seq:
 *     obj.freeze    -> obj
 *
 *  Prevents further modifications to <i>obj</i>. A
 *  <code>RuntimeError</code> will be raised if modification is attempted.
 *  There is no way to unfreeze a frozen object. See also
 *  <code>Object#frozen?</code>.
 *
 *  This method returns self.
 *
 *     a = [ "a", "b", "c" ]
 *     a.freeze
 *     a << "z"
 *
 *  <em>produces:</em>
 *
 *     prog.rb:3:in `<<': can't modify frozen array (RuntimeError)
 *      from prog.rb:3
 */

VALUE
rb_obj_freeze(VALUE obj)
{
    if (!OBJ_FROZEN(obj)) {
    if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
        rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
    }
    OBJ_FREEZE(obj);
    if (SPECIAL_CONST_P(obj)) {
        if (!immediate_frozen_tbl) {
        immediate_frozen_tbl = st_init_numtable();
        }
        st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
    }
    }
    return obj;
}