Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the encoding of stdin in Ruby

Tags:

ruby

encoding

I want my script to be able to take input from stdin where the data might be provided in UTF8 or UTF16 encoding.

something like:

datasource | my-script -e utf8

How do I set the external_encoding of stdin ?

like image 612
Martin Avatar asked Nov 01 '22 09:11

Martin


1 Answers

In the first line of the script where you define ruby as the interpreter, you may add the --encoding utf-8 parameter in order specify the stdin encoding.

Example:

#!/usr/bin/env ruby --encoding utf-8

text = ARGF.read

From man ruby:

 -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.
like image 121
fiedl Avatar answered Nov 15 '22 05:11

fiedl