How to Convert a String to Lowercase or Uppercase in Ruby

Nurudeen Ibrahim Feb 02, 2024
  1. Convert a String to Uppercase Using the upcase Method
  2. Convert a String to Lowercase Using the downcase Method
How to Convert a String to Lowercase or Uppercase in Ruby

Converting a string from one case to another is a common task in Programming, and Ruby provides two methods that help with that, the upcase and the downcase.

Convert a String to Uppercase Using the upcase Method

str = 'Hello World'
puts str.upcase

Output:

HELLO WORLD

Convert a String to Lowercase Using the downcase Method

Example Code:

str = 'Hello World'
puts str.downcase

Output:

hello world

It’s worth mentioning that the upcase! and downcase! methods work the same way as upcase and downcase respectively but instead mutate the original string.

str_1 = 'Hello World'
str_2 = 'Hello World'

puts str_1.upcase!
puts str_1

puts str_2.downcase!
puts str_2

Output:

HELLO WORLD
HELLO WORLD

hello world
hello world

Related Article - Ruby String

Related Article - Ruby Method