Q:

Ruby program to make string immutable

belongs to collection: Ruby Strings Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Strings Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to compare two strings using the Equa... >>
<< Ruby program to demonstrate the string concatenati...