What is Encoding.name in Ruby?
Overview
The name method of an encoding instance returns the string representation of an encoding. The string it returns is the name of the encoding.
Syntax
str.encoding.name
Parameter
str: This parameter gets the encoding of a string.
Return value
A string is returned, which is the encoding’s name.
Code example
# get and print the names of# some encodings in Ruby# encode some strings.str1 = "Welcome to Edpresso".encode(Encoding::ISO_8859_1)# get name of encodingputs "The encoding name for \"#{str1}\" is #{str1.encoding.name}"
Explanation
- Line 5: We create a string and encode it in
ISO_8859_1. - Line 8: We print the string and get the name of its encoding using the
nameproperty.