Much like a vestigial tail, Ruby has a character literal syntax which is funny-looking and largely useless. In the interests of completeness, let’s see if we can find an excuse to use it.
(Editor’s note, March 2020: Wow, my 2012 production values were… not what they are today )
Unlike languages such as C, Ruby does not have a character data type distinct from the String type. However, Ruby does have a character literal syntax. To quote a single character, just precede the character with a question mark:
?c # => "c" ?q # => "q"
As you can see, the result of quoting a single character is simply a one-character-long string.
What is this good for? Well, other than saving a single character, not a whole lot, but I thought I'd include it for completeness.
One way in which this might be useful is for making strings which should only ever be one character long visually distinct. For instance, 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 literals serve to make it clear to the reader that the input being considered will only ever be a single character.
That's all for today. Happy hacking!