Trusted answers to developer questions

How to convert a string to lowercase in Ruby

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Overview

In Ruby, we use the downcase method to convert a string to lowercase. This method takes no parameters and returns the lowercase of the string that calls it. It returns the original string if no changes are made.

Note: This method does not affect the original string.

Syntax

str.downcase

str is the name of the string to convert.

Parameters

This method requires no parameters.

Return value

This method returns the lowercase of the string that calls it, in this case, str. It returns the original string if there is no conversion.

Code example

# create strings
str1 = "WELCOME"
str2 = "to"
str3 = "EDpreSSO"
# downcase strings
a = str1.downcase
b = str2.downcase
c = str3.downcase
# print results
puts a
puts b
puts c
puts str1
puts str2
puts str3

Explanation

In the code above, we create some strings and convert them to lowercase using the downcase method.

We also notice from the code above that nothing changes for the string str2. This is because it is already in lowercase.

Also, observe that no changes are made to the original string.

RELATED TAGS

ruby
string
lowercase
Did you find this helpful?