How to permanently capitalize a string in Ruby

Overview

Capitalizing a string in Ruby means changing the first letter of the string to upper case. The capitalize method is useful in this regard. However, capitalize! is the best method to change the string permanently.

Syntax

str.capitalize!

Parameters

None.

Return value

The value returned is the string with its first letter in the upper case.

Code

We will create some strings and capitalize them permanently. See the code example below:

# create strings
str1 = "edpresso"
str2 = "is"
str3 = "the"
str4 = "best"
# capitalize them
a = str1.capitalize!
b = str2.capitalize!
c = str3.capitalize!
d = str4.capitalize!
# print changed strings
puts str1
puts str2
puts str3
puts str4

Free Resources