From: "Dan0042 (Daniel DeLorme)" <noreply@...>
Date: 2022-11-30T18:21:30+00:00
Subject: [ruby-core:111095] [Ruby master Bug#18899] Inconsistent argument handling in IO#set_encoding

Issue #18899 has been updated by Dan0042 (Daniel DeLorme).





Naively, I would have expected "binary:utf-8" to take arbitrary input and force the encoding to UTF-8, and "utf-8:utf-8" to read and validate the input as UTF-8.

Neither does what I expected. `��\_(���)_/��`



----------------------------------------

Bug #18899: Inconsistent argument handling in IO#set_encoding

https://bugs.ruby-lang.org/issues/18899#change-100360



* Author: javanthropus (Jeremy Bopp)

* Status: Open

* Priority: Normal

* ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN

----------------------------------------

`IO#set_encoding` behaves differently when processing a single String argument than it does when processing 2 arguments (whether Strings or Encodings) in the case where the external encoding is being set to binary and the internal encoding is being set to any other encoding.



This script demonstrates the resulting values of the external and internal encodings for an IO instance given different ways to equivalently call `#set_encoding`:



```ruby

#!/usr/bin/env ruby





def show(io, args)

  printf(

    "args: %-50s  external encoding: %-25s  internal encoding: %-25s\n",

    args.inspect,

    io.external_encoding.inspect,

    io.internal_encoding.inspect

  )

end



File.open('/dev/null') do |f|

  args = ['binary:utf-8']

  f.set_encoding(*args)

  show(f, args)



  args = ['binary', 'utf-8']

  f.set_encoding(*args)

  show(f, args)



  args = [Encoding.find('binary'), Encoding.find('utf-8')]

  f.set_encoding(*args)

  show(f, args)

end

```



This behavior is the same from Ruby 2.7.0 to 3.1.2.







-- 

https://bugs.ruby-lang.org/

 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/