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 encoding
puts "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 name property.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources