Skip to main content
Ruby

Ruby — All about case

By October 10, 2013No Comments



In ruby we can manipulate the case of strings with methods:

  • capitalize
  • upcase
  • downcase

Working with irb the interactive ruby environment we can test this easily. First we will create a local variable. Local variable start with an _ or lowercase character.

name = "sAlly"

So we now have the local variable name that has a string value of sAlly. If we only want to print the variable and nothing else to the screen we can use

puts name

If we name to manipulate the case we can append the correct method

puts name.captialize

This prints Sally

puts name.upcase

This will print SALLY

puts name.downcase

Will print, …. you guessed it .. sally

If we need an additional text string to be printed with the variable we can delimit the variable when it is quoted.

puts "Welcome #{name}"

This will print Welcome sAlly

puts "Welcome #{name.capitalize}"

Will print Welcome Sally