我希望我的脚本能够从 stdin 获取输入,其中数据可能以 UTF8 或 UTF16 编码提供。
就像是:
datasource | my-script -e utf8
如何设置 stdin 的 external_encoding ?
在定义为解释器的脚本的第一行中,您可以添加参数以指定 stdin 编码。ruby--encoding utf-8
#!/usr/bin/env ruby --encoding utf-8
text = ARGF.read
Run Code Online (Sandbox Code Playgroud)
man ruby:Run Code Online (Sandbox Code Playgroud)-E external[:internal] --encoding external[:internal] Specifies the default value(s) for external encodings and internal encoding. Values should be separated with colon (:). You can omit the one for internal encodings, then the value (Encoding.default_internal) will be nil.
| 归档时间: |
|
| 查看次数: |
896 次 |
| 最近记录: |