红宝石 |字符串编码方式
encoding是 Ruby 中的 String 类方法,用于返回表示对象编码的 Encoding 对象。
Syntax: str.encoding
Parameters: Here, str is the given string.
Returns: An encoding object.
示例 1:
# Ruby program to demonstrate
# the encoding method
# Taking a string and
# using the method
puts "R\xC3\xA9sum\xC3\xA9".encoding
puts "Ruby\n\n".encoding
输出:
UTF-8
UTF-8
示例 2:
# Ruby program to demonstrate
# the encoding method
# Taking a string and
# using the method
puts "Sample".encoding
puts "Input\n\n".encoding
输出:
UTF-8
UTF-8