Object#frozen?

obj.frozen? -> true or false
Returns: true or false · Updated March 13, 2026 · Core Classes
objects immutability core

The frozen? method checks whether an object is frozen. A frozen object cannot be modified—any attempt to change its state will raise a FrozenError. This is useful for implementing immutability patterns, protecting constants, and ensuring data integrity.

Syntax

object.frozen?

Parameters

This method takes no parameters.

Examples

Basic usage

str = "hello"
str.frozen?
# => false

sym = :hello
sym.frozen?
# => true

Understanding frozen literals

# String literals are frozen in Ruby 3.0+
"hello".frozen?
# => true

# Variables holding frozen objects can be reassigned
a = "hello"
a.frozen?
# => true

a = "world"  # reassignment works
a.frozen?
# => true

Freezing objects explicitly

arr = [1, 2, 3]
arr.frozen?
# => false

arr.freeze
arr.frozen?
# => true

# Attempting to modify raises FrozenError
arr << 4
# => FrozenError (array is frozen)

Checking object identity

a = "hello"
b = "hello"

# In Ruby, identical string literals may share the same object
a.object_id == b.object_id
# => true (depends on Ruby's internal string deduplication)

a.frozen?
# => true (string literals are frozen)

Common Patterns

Protecting constants

CONFIG_KEYS = %w[api_key secret token].freeze

# Without freeze, someone could do:
# CONFIG_KEYS << "admin"  # would work!

CONFIG_KEYS.frozen?
# => true

Conditional modification

def process_data(data)
  # Work with a copy if the original is frozen
  working_copy = data.frozen? ? data.dup : data
  working_copy << "processed"
end

original = "test".freeze
process_data(original)
# => "testprocessed" (original unchanged)

Debugging frozen state

def assert_mutable!(object)
  raise "Object is frozen!" if object.frozen?
end

Understanding Ruby’s Freezing Mechanism

Ruby’s freezing mechanism is fundamental to its object model. When you freeze an object, you prevent any modifications to its internal state. This is particularly important in concurrent programming where multiple threads might access the same data structures.

In Ruby 3.0 and later, string literals are automatically frozen by default. This was a significant change that improved performance and encouraged immutability. You can still create mutable strings dynamically, but string literals are always frozen.

The freeze method affects the object itself, not the variable holding it. This means you can reassign a variable pointing to a frozen object, but you cannot modify the frozen object through any reference.

Performance Implications

Frozen objects can be more memory-efficient because Ruby can deduplicate identical frozen strings. When multiple variables hold the same string value, Ruby may store only one copy in memory, with all variables referencing the same object. This reduces memory usage significantly in applications with many string literals.

Additionally, frozen objects can sometimes be processed faster because the runtime doesn’t need to worry about concurrent modifications. In multi-threaded applications, this can lead to performance improvements.

Errors

FrozenError

When you try to modify a frozen object, Ruby raises a FrozenError:

arr = [1, 2, 3].freeze
arr << 4
# => FrozenError (array is frozen)

The error message includes the object that was frozen and provides context for debugging.

See Also