What is string.capitalize() in Ruby?

The string.capitalize() function in Ruby does not modify the original string, but instead returns a copy of the original string, in which the first letter is capitalized and the remaining letters are lower case.

Figure 1 shows the visual representation of the string.capitalize() function.

Figure 1: Visual representation of string.capitalize() function

Syntax

string_name.capitalize()
# string_name is the name of the string to be capitalized

Parameter

This function does not require a parameter.

Return value

This function returns the copy of the original string, in which the first letter is capitalized and the remaining letters are lower case.

Example

The following code shows how to use the string.capitalize() function.

# working example of string.capitalize()
print "('hello').capitalize() : ", ('hello').capitalize(), "\n"
print "('educative').capitalize() : ", ('educative').capitalize(), "\n"

Free Resources