Currently the handling of string encoding is asymmetric: All strings that are received from the database are properly tagged with the encoding of the connection. However all strings sent to the database are sent in the internal binary string representation, regardless to which encoding the given string is tagged.
In order to respect the encoding of strings sent to the database, all strings should be converted to the connection encoding, before transmission. This should happen internally in ruby-pg and only if the given string encoding is differently to the connection encoding.
Currently the user is responsible for the right encoding, but it would fit better into ruby-pg.
Since PG::Coder classes are independent to any database connection,
PG::Coder#encodeshould get a second parameter, which defines the destination string encoding for the type encoding process. It could be used like this:
e = PG::TextEncoder::Array.new e.encode(['nový'], conn.internal_encoding).encoding # => #<Encoding:UTF-8>