Ruby

Search for '{{search_term}}'

How to convert a string to UPPERCASE or lowercase in Ruby?

CMOS is the Code-Maven Open Source podcast that also includes video interviews. Subscribe to this feed RSS feed with your Podcast listener app or via iTunes iTunes.

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 = 'Foo'
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

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus