How to Convert an Array of Strings to Uppercase in Ruby?

Find out how to make all strings uppercase in a Ruby array of strings

In Ruby, you can make all strings in an array of strings uppercase by calling the String#upcase method on every element of the array using Array#map, for example, like so:

arr = ["foo", "Bar", "bAz"]
new_arr = arr.map { |item| item.upcase }

print new_arr #=> ["FOO", "BAR", "BAZ"]

You could also shorten this by using the &: syntax, for example, like so:

arr = ["foo", "Bar", "bAz"]
new_arr = arr.map(&:upcase)

print new_arr #=> ["FOO", "BAR", "BAZ"]

Using either of the above would create a new array with all strings in the array in uppercase. If you want to mutate the original array instead, then you simply need to use Array#map! instead of Array#map, for example, like so:

arr = ["foo", "Bar", "bAz"]
arr.map!(&:upcase)

print arr #=> ["FOO", "BAR", "BAZ"]

Hope you found this post useful. It was published . Please show your love and support by sharing this post.