How to know if an encoding is ASCII compatible

Overview

An encoding is ASCII compatible if it is a standard for electronic communication. To know if an encoding is ASCII compatible, we can use the ascii_compatible? property.

Syntax

encoding.ascii_compatible?

Return value

The value returned is a boolean. A true is returned if the encoding is ASCII compatible. Otherwise, false is returned.

Example

# get some encodings
e1 = Encoding::UTF_8
e2 = Encoding::UTF_32
e3 = Encoding::UTF_16BE
e4 = Encoding::UTF_32LE
# check if they are ASCII compatible
puts e1.ascii_compatible? # true
puts e2.ascii_compatible? # false
puts e3.ascii_compatible? # false
puts e4.ascii_compatible? # false

Explanation

  • Line 2–5: We get some encodings.
  • Line 8–11: We check if the encodings are ASCII compatible. Then we print the result to the console.

Free Resources