Ruby
Programming
String Manipulation
Coding Tips
Case Conversion

How to convert a string to lower or upper case in Ruby

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Ruby, converting strings to either uppercase or lowercase is a fundamental task for any developer. This capability can be extremely useful in situations such as comparing user input in a case-insensitive manner or for formatting outputs. Ruby provides several methods to accomplish these conversions, making it straightforward to manipulate string case according to your needs.

Understanding .upcase and .downcase

The most commonly used methods to change the case of a string in Ruby are .upcase and .downcase.

  • .upcase: This method is used to convert all the lowercase characters in a string to their uppercase counterparts. It returns a new string with all characters converted to uppercase.
  • .downcase: This method converts all the uppercase characters in a string to their lowercase counterparts, producing a new string where all characters are lowercase.

Example Usage

Here's a basic example demonstrating these methods:

ruby
1original_string = "Hello World"
2lowercased_string = original_string.downcase
3uppercased_string = original_string.upcase
4
5puts lowercased_string  # Output will be "hello world"
6puts uppercased_string  # Output will be "HELLO WORLD"

Case Conversion with Localization

Ruby 2.4 and newer versions support case mapping in accordance with the Unicode standard, also considering locale-specific cases. This is particular useful for characters that do not fit into the simple A-Z categorization.

  • .upcase and .downcase with Locale: Both these methods can take a locale code as an argument. This tells Ruby to consider the locale (language and region) when converting the case of the string.

Example with Localization

ruby
1string = "straße"  # 'ß' is a German letter
2
3# Using just upcase
4puts string.upcase  # Output will be "STRASSE"
5
6# Using upcase with German locale
7puts string.upcase(:de)  # Output will be "STRASSE" (as 'ß' turns into 'SS')

Note: Locale-sensitive case mapping is not frequently used and can be considered an advanced feature. The default locale is English unless specified otherwise.

Preservation of Original String

Both .downcase and .upcase methods do not modify the original string but instead return a new string with the changes. If you need to change the original string itself, Ruby provides the bang-method versions of these methods: .downcase! and .upcase!.

ruby
1original_string = "Hello World!"
2
3original_string.downcase!
4puts original_string  # Output will be "hello world!"
5
6original_string.upcase!
7puts original_string  # Output will be "HELLO WORLD!"

Table Summarizing Key Methods

MethodDescriptionModifies Original?
.upcaseConverts all lowercase letters to uppercase.No
.downcaseConverts all uppercase letters to lowercase.No
.upcase(:locale)Converts to uppercase considering the locale.No
.downcase(:locale)Converts to lowercase considering the locale.No
.upcase!Converts original string to uppercase.Yes
.downcase!Converts original string to lowercase.Yes

Common Use-Cases

  1. Case-Insensitive Comparison: When comparing two strings where the case might essentially be the same but typed differently.
  2. Data Normalization: Especially important in data processing where uniformity in the case of strings is necessary.
  3. User Input Correction: Autocorrecting user inputs, such as converting email addresses to a specific case (commonly lowercase).

By understanding how to effectively utilize .upcase and .downcase and their variations, you can handle string manipulations more efficiently in Ruby, catering to both functionality and localization needs. Utilize these tools to maintain consistency and accuracy of data when dealing with user inputs or displaying information.


Course illustration
Course illustration

All Rights Reserved.