Learn Ruby – How To Convert a String to Lower or Uppercase?

In Ruby, a String contains and manipulates an arbitrary sequence of bytes, typically representing characters. When you want to change the contents of a String in Ruby, it should be noted that methods with the name ending in “!” modify the String that is receiving this method, while those without “!” return a new String.

How to convert a string to Lower or Uppercase of a String in Ruby

To convert the content to lowercase, use .downcase:

Similarly, to transform everything to uppercase, use .upcase:

If you want to capitalize only the first letter of a String, use the .capitalize method:

You can also use .titleize to ensure that the first letter of each word in the String will be uppercase:

Remember that if you add the exclamation point in any of these methods, the original String will be modified, as shown below:


Including support for Unicode, and ASCII in capitalization methods in Ruby

These methods operate natively in ASCII. To add support for Unicode, is necessary to install a gem, as showed below:

Do you know other ways to perform this case changes on a String in Ruby? Share your comments in the section below!

If you want to explore other questions about this programming language, you can check out our Ruby programming videos about Ruby. Below we show some examples:

You can also subscribe to some channels that broadcast programming in Ruby, such as the following:

Ruby string uppercase lowercase capitalize programming j-mcnally



Ruby string uppercase lowercase capitalize programmingjosephwilk



Another cool way to find out interesting things about Ruby is to access our project page!

About author

About Author Dr. Michael Jurgen Garbade is the founder of LiveEdu.TV, Kyuda, Education Ecosystem. He is future Venture Capitalist, Future Politician and always on the lookout for the Next Big Challenge. Obtained Masters in business administration and physics, and a Ph.D. in finance with professional work experience in high-paced environments at Fortune 500 companies like Amazon and General Electric. Expertize: Python, PHP, Sencha Touch & C++, SEO, Finance, Strategy & E-commerce. He speaks English and German and has worked in the US, Europe, and Asia. At Education Ecosystem he is the CEO and runs business operations.