Hey, It's Alexandre Calaça here. Hope you're good.
Today, let's learn and practice how to convert a string to lowercase in Ruby.
In Ruby, the
downcase method is used to convert a string to lowercase. It returns a copy of self (current object) with all characters downcased.
In the previous example, str is the variable name.
downcase method doesn't require parameters.
It returns a new lowercase string of the caller. If no conversation is necessary, It returns the original string.
It's possible to return and modify self (current caller) if we use the
bang operator after the method.
Let's check the examples and learn from them.
In the following examples, it's noticeable that:
downcasemethod doesn't modify the original string;
- It returns a new lowercase string of the caller;
"ALEXANDRE" my_string.downcase => "alexandre" my_string => "ALEXANDRE"my_string =
it's possible to see that:
downcasemethod returns the same string if no conversion is necessary.
"alexandre calaca" my_new_string.downcase => "alexandre calaca"my_new_string =
bangoperator allows the
downcasemethod to modify self.
irb(main):012:0> my_string = "AlExAnDrE" irb(main):013:0> my_string => "AlExAnDrE" irb(main):014:0> my_string.downcase! => "alexandre" irb(main):015:0> my_string => "alexandre"
In this article, it was possible to learn and practice how to convert a string to lowercase in Ruby. There were excellent examples.
That's all for today. Reach me out if you have any questions, guys.