From: shugo@... Date: 2015-12-17T02:23:16+00:00 Subject: [ruby-core:72201] [Ruby trunk - Bug #11827] [Feedback] StringIO with set_encoding broken with enabled frozen string literal Issue #11827 has been updated by Shugo Maeda. Status changed from Open to Feedback deepj # wrote: > I'm not sure if this the following problem is an expected behavior. But I guess, it is a bug when I run the code below. It throws `set_encoding': can't modify frozen String (RuntimeError). > > ~~~ > # frozen_string_literal: true > > io = StringIO.new('') > io.set_encoding(Encoding::BINARY) > ~~~ > > I wouldn't expect this kind of behavior. What behavior do you expect? StringIO is a wrapper to provide IO-like interface for String, so the argument of `StringIO.new` should not be copied. ```ruby s = "foo" io = StringIO.new(s) io.putc(?b) p s #=> "boo" ``` If you specify writable mode explicitly, you can get an exception earlier: ```ruby # frozen_string_literal: true io = StringIO.new("", "w") #=> Errno::EACCESS ``` ---------------------------------------- Bug #11827: StringIO with set_encoding broken with enabled frozen string literal https://bugs.ruby-lang.org/issues/11827#change-55612 * Author: deepj # * Status: Feedback * Priority: Normal * Assignee: * ruby -v: ruby 2.3.0preview2 (2015-12-11 trunk 53028) [x86_64-darwin15] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- I'm not sure if this the following problem is an expected behavior. But I guess, it is a bug when I run the code below. It throws `set_encoding': can't modify frozen String (RuntimeError). ~~~ # frozen_string_literal: true io = StringIO.new('') io.set_encoding(Encoding::BINARY) ~~~ I wouldn't expect this kind of behavior. -- https://bugs.ruby-lang.org/