Table of contents
1.
Introduction
2.
Character Literals In ruby
3.
Character Literals In ruby 1.8 & 1.9
4.
Frequently Asked Questions
4.1.
What are literals in Ruby?
4.2.
Are literals objects in Ruby?
4.3.
What is character Literals In ruby?
4.4.
How to write character Literals In ruby?
4.5.
What does "?C" character Literals In ruby represent?
5.
Conclusion
Last Updated: Mar 27, 2024

Character Literals In ruby

Author Alok Pandey
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Character Literals In ruby 1.8 & 1.9

In Between Ruby 1.8 and Ruby 1.9, the interpretation of the character literal has changed.

In Ruby 1.8, character literals in ruby evaluate the integer encoding of the specified character. ?C, for example, is the same as 68 because the ASCII encoding for the capital letter C is the integer 68. 

In Ruby 1.8, the character literal syntax only works with ASCII and single-byte characters. 

In Ruby 1.9 and later, characters are simply strings of length 1. That is, the literal ?A is the same as the literal 'A', and there is no need for this character's literal syntax in new code.

In Ruby 1.9, the character literal syntax works with multibyte characters and can also be used with the \u Unicode escape (though not with the multicodepoint form \u{a b c}): 

?\u20AC == ?€ # => true: Ruby 1.9 only 
?€ == "\u20AC" # => true

The character's literal syntax can actually be used with any of the character's escapes

?\t # Character literal for the TAB character 
?\C-x # Character literal for Ctrl-X 
?\111 # Literal for a character whose encoding is 0111 (octal)

 

Check out this problem - Shortest Common Supersequence.

Frequently Asked Questions

What are literals in Ruby?

In the Ruby programming language, a literal is a special syntax that produces an object of a particular type. For instance, the literal 98 produces a Fixnum object. There are various types of string literal.

Are literals objects in Ruby?

There are no object literals in Ruby. The object-oriented language Ruby is based on classes. Each object is an instance of a class, and classes are in charge of making instances of their own objects.

What is 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.

How to write character Literals In ruby?

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'

What does "?C" character Literals In ruby represent?

?C, is the same as 68 because the ASCII encoding for the capital letter C is the integer 68. 

Conclusion

In this article, we have extensively discussed Character Literals In ruby

We hope that this blog has helped you enhance your knowledge regarding Character Literals In ruby and if you would like to learn more, check out our articles on

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass