Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

String#swapcase

str.swapcase → new_str
str.swapcase([options]) → new_str

Returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase.

See String#downcase for meaning of options and use with different encodings.

Example
"Hello".swapcase          #=> "hELLO"
"cYbEr_PuNk11".swapcase   #=> "CyBeR_pUnK11"

Ruby version changes

  1. 3.0
See also #swapcase! #sum #succ! #to_c also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1