Ruby:
Converting a string to lower case
How to:
# Using the downcase method
my_string = "Hello World!"
puts my_string.downcase # => "hello world!"
# Using downcase! for in-place transformation
my_string = "Hello World!"
my_string.downcase!
puts my_string # => "hello world!"
Deep Dive
Historically, case conversion has been a staple in programming languages to ensure text uniformity. It supports case-insensitive comparisons and searches, hence its importance.
The downcase
and downcase!
methods in Ruby stem from the language’s principle of providing both non-destructive and destructive methods for string manipulation. The non-destructive downcase
returns a new string, leaving the original untouched, while the destructive downcase!
modifies the original string in place, which can be more memory efficient.
There are alternatives for cases when locale-specific rules apply. String#mb_chars
combined with ActiveSupport::Multibyte::Chars#downcase
from the Rails ActiveSupport library can handle more complex situations like characters with accents or other diacritical marks:
require 'active_support/core_ext/string/multibyte'
my_string = "ÄÖÜ"
puts my_string.mb_chars.downcase # => "äöü"
As for implementation, Ruby’s downcase
and downcase!
internally use Unicode mapping to convert each character of the string to its lowercase equivalent.
See Also
- Ruby documentation for
downcase
anddowncase!
: Ruby Doc downcase, Ruby Doc downcase! - For complex case conversions, see the ActiveSupport Core Extensions: ActiveSupport String