From: Alex Young Date: 2011-09-21T00:18:22+09:00 Subject: StringIO and encodings This surprised me: $ ruby -v ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux] $ ruby -e 'p "".encoding' # $ cat a.rb # encoding: utf-8 require 'stringio' s = StringIO.new a = "abc" s.puts(a) p :string => a.encoding p :stringio => s.string.encoding $ ruby a.rb {:string=>#} {:stringio=>#} # <- WTF? $ cat b.rb # encoding: utf-8 require 'stringio' s = StringIO.new("") # <- note the constructor parameter a = "abc" s.puts(a) p :string => a.encoding p :stringio => s.string.encoding $ ruby b.rb {:string=>#} {:stringio=>#} # <- better! I presume that what's going on here is a simple matter of defaults. If you don't pass an initial string to StringIO.new, it constructs one for itself using the default encoding from the locale, and the encoding coercion rules mean that the internal string's encoding will always be the same. StringIO.new has no knowledge of the file encoding at the location it's called from. This behaviour seems odd to me. I think a better behaviour would be *either* to always force a string parameter, so that it never has to pick a default encoding itself, *or* that it should not make itself an internal string on #new, but instead #dup the first string it gets passed as a parameter to #write or #puts and use that instead. Thoughts? -- Alex -- Posted via http://www.ruby-forum.com/.