What is string.empty? in Ruby?

The string.empty? function in Ruby returns a true value if the string is empty; otherwise, it returns false.

Figure 1 shows the visual representation of the string.empty? function.

Figure 1: Visual representation of string.empty? function

Syntax

string_name.empty?
# string_name is the name of the string

Parameter

This function does not require a parameter.

Return value

If the string is empty, the string.empty? function returns a true value. Otherwise, it returns false.

Example

The following code widget shows how the string.empty? function is used.

# string with value
str_1='Hello'
print "The string1('Hello') is empty : ", str_1.empty?, "\n"
# empty string
str_2 = ''
print "The string2('') is empty : ", str_2.empty?, "\n"

Free Resources