liveBook · Manning

Case conversion

As explained in The Well-Grounded Rubyist, Fourth Edition

A case-changing method is a string or symbol operation that produces text with altered letter case.

Case conversion changes the letter case of a string. Ruby provides methods for raising, lowering, swapping, and capitalizing case; each has a receiver-modifying bang equivalent, including capitalize!. section 8.1.5

The principal transformations are:

  • upcase converts "David A. Black" to "DAVID A. BLACK". section 8.1.5
  • downcase converts it to "david a. black". section 8.1.5
  • swapcase reverses the case of each letter, producing "dAVID a. bLACK". section 8.1.5
  • capitalize transforms "david" into "David". section 8.1.5
>> string = "David A. Black"
=> "David A. Black"
>> string.upcase
=> "DAVID A. BLACK"
>> string.downcase
=> "david a. black"
>> string.swapcase
=> "dAVID a. bLACK"

Compare upcase, downcase, and swapcase directly in an irb-sized example. section 8.1.5

>> string = "david"
=> "david"
>> string.capitalize
=> "David"

See capitalization applied to a lowercase string. section 8.1.5

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Demonstrates four case methods and notes receiver-modifying bang equivalents without detailing their edge cases.
Examples
Uses irb examples with "David A. Black" and "david" to demonstrate uppercase, lowercase, swapped, and capitalized results.
What it assumes
Uses Ruby strings and method calls as the starting point for demonstrating transformations.
What you can do after
Apply via Ruby String methods to produce altered-case text or use their bang equivalents for receiver modification.
How it connects
Relates case conversion to string transformation, formatting transformation, and content transformation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.