Ruby 中 Python 的 encode() 的等价物是什么

What is the equivalent of Python's encode() in Ruby

我正在将 Python 脚本传输到 Ruby。

python方法'encode()'经常使用。示例:

apikey.encode()
encoded_payload = json.dumps(payload).encode()

看起来 Ruby 等价物有一些必需的参数需要添加。 UTF-8 或 ISO-8859-1。哪个符合标准 python encode().

Python 的 str.encode() 默认为 utf-8,所以请使用它。

Ruby:

   string = "some string".encode
   string.encoding

Encoding:UTF-8

所以Ruby的default是UTF-8

Python default 也以 UTF-8 编码

因此无需添加参数,两者默认值相同:UTF-8


完整性:

如果有人想更改默认设置 UTF-8,您可以在 python(参见 documentary and supported encodings)中通过以下方式进行更改:

string.encode(encoding='UTF-8')

同样在Ruby:

string.encode("UTF-8")

支持Rubyencodings