How to convert a string to UPPERCASE or lowercase in Ruby?
Ruby strings have methods to convert them to uppercase and lowercase. The method names of the methods are upcase and downcase respectively.
Calling the downcase or upcase method on a string will return the lowercase or uppercase version of the string, but the original variable won't change.
name = 'Ruby' puts name # Ruby puts name.downcase # ruby puts name.upcase # RUBY puts name # Ruby
Calling the same methods followed by an exclamation mark will both return lowercase/uppercase version of the string and will also change the content of the variable.
puts name.downcase! # ruby puts name # ruby puts name.upcase! # RUBY puts name # RUBY
Full example
examples/ruby/case.rb
name = 'Foo' puts name # Ruby puts name.downcase # ruby puts name.upcase # RUBY puts name # Ruby puts name.downcase! # ruby puts name # ruby puts name.upcase! # RUBY puts name # RUBY

Published on 2015-10-29