×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to make string immutable

Last Updated : December 15, 2025

Problem Solution

In this program, we will make a string immutable using the freeze() method. After calling the freeze() method with a string variable, the string cannot be modified.

Program/Source Code

The source code to make the string immutable is given below. The given program is compiled and executed successfully.

# Ruby program to make string immutable

str = "Hello ";

str = str<< "World";

print str;

str.freeze();

#Below state will generate error.
#str = str<< " Hello Again";

Output

Hello World

Explanation

In the above program, we used the freeze() method with a string. The freeze() method is used to make a string immutable. After calling the freeze() method with string, we cannot modify the string variable.

Ruby Strings Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.