Alexandre Calaça
Alexandre Calaça's Blog

Alexandre Calaça's Blog

Ruby: How to convert a string to uppercase?

Ruby: How to convert a string to uppercase?

How to convert a string to uppercase in Ruby?

Alexandre Calaça's photo
Alexandre Calaça
·Jul 5, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Greetings
  • Introduction
  • Solution
  • Syntax
  • Parameters
  • Return value
  • Coding time
  • Errors
  • Conclusion


Hey, It's AC (Alexandre Calaça) here. Hope you're good. Let's get down to business. Shall we?


Today, let's learn and practice how to convert a string to uppercase in Ruby.


In Ruby, the upcase method is used to convert a string to uppercase.

The upcase method is immutable, it means it returns a copy of self (current object) with all characters uppercased, so, just to be clear, the original string stays the same.

The mutable version is used with a bang sign at the end, upcase!



In this case, my_string is the string we are interested to uppercase.


The upcase method doesn't require any parameters.

Return value

Since it is an immutable method, it returns a new uppercased string of the caller. If no conversion is necessary, it returns the unchanged original string.

Again, it's possible to return and modify self (current caller) if we use the bang operator after the method: upcase!.

Coding time

Let's go over the examples.

irb(main):001:0> my_string = "alexandre"
irb(main):002:0> my_string.upcase
irb(main):003:0> my_string
=> "alexandre"

In the previous examples, it's noticeable that:

  • The upcase method doesn't modify the original string;
  • It returns a new uppercased string of the caller;

irb(main):006:0> my_new_string = "ALEXANDRE CALACA"
irb(main):007:0> my_new_string.upcase

it's possible to see that:

  • The upcase method returns the same string if no conversion is necessary.

> my_string = "AlExAnDRe CalACa"
=> "AlExAnDRe CalACa" 
> my_string.upcase
> my_string

The bang operator allows the upcase method to modify self.


  • NoMethodError
    If the method is not used with a String object
2.7.3 :026 > array.upcase
NoMethodError (undefined method `upcase' for [1, 2, 3]:Array)
2.7.3 :027 > 4.upcase
NoMethodError (undefined method `upcase' for 4:Integer)
  • ArgumentError
    If it's provided unnecessary arguments.
2.7.3 :028 > my_string
2.7.3 :029 > my_string.upcase("test")
ArgumentError (invalid option)


In this article, it was possible to learn and practice how to convert a string to uppercase in Ruby. There are excellent examples.

That's all for today. Reach me out if you have any questions, guys.

Share this