ADVERTISEMENT
ADVERTISEMENT

Ruby program to make string immutable

Ruby Example: Write a program to make string immutable.
Submitted by Nidhi, on December 31, 2021

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!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.