Introduction
A character literal, such as 'c,' is a collection of characters or escape sequences wrapped in single quote mark symbols. The letter L can be used to prefix a character literal, such as L'c'. An ordinary or narrow character literal does not have the L prefix. A wide-character literal starts with the letter L. A multicharacter literal is a character literal that comprises more than one character or escape sequence (excluding single quotes ('), backslashes (\), and new-line characters).
Character Literals In ruby
A single character can literally be included in a Ruby program by preceding it with a question mark. Ruby does not have a separate class to represent single characters, despite having a character literal syntax.
Unlike languages like C, Ruby does not have a character data type distinct from the string type. However, Ruby does have Character Literals syntax.
To write a single character proceed with the character with a question mark.
Example:
?c # represents a single character 'c'
?q # represents a single character 'q'
As mentioned in the above example, the result of quoting a single character is simply a one-character-long string.
One way in which this character literal In ruby might be useful is for making strings that should only ever be one character long and visually distinct. For example, let's say we're checking input to see if the user typed either "y" or "n" in response to a prompt.
case $stdin.getc.downcase
when ?y then puts "Proceeding..."
when ?n then puts "Aborting."
else puts "I don't understand"
end
Here, the use of single-character literal in ruby makes it clear to the user that the input should be single characters only.