Issue #5836 has been updated by Michael Fellinger.


I've reduced the code a bit, calling `force_encoding` seems to cause it, and calling `valid_encoding?` repairs the damage.

 s = 'abc'
 p [s.slice(3), s.slice(3, 1)]
 [nil, ""]
 
 s.force_encoding 'euc-jp'
 p [s.slice(3), s.slice(3, 1)]
 [nil, nil]
 
 s.valid_encoding?
 p [s.slice(3), s.slice(3, 1)]
 [nil, ""]
 
 s.force_encoding 'euc-jp'
 p [s.slice(3), s.slice(3, 1)]
 [nil, nil]
----------------------------------------
Bug #5836: #slice results inconsistent after #force_encoding to a valid encoding
https://bugs.ruby-lang.org/issues/5836

Author: Brian Ford
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]


I was not able to reduce this further. After calling #force_encoding, #slice returns inconsistent results:

ruby-1.9.3-p0 :001 > RUBY_DESCRIPTION
 => "ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]" 
ruby-1.9.3-p0 :002 > s = "abc"
 => "abc" 
ruby-1.9.3-p0 :003 > s.size
 => 3 
ruby-1.9.3-p0 :004 > s.encoding
 => #<Encoding:UTF-8> 
ruby-1.9.3-p0 :005 > s.valid_encoding?
 => true 
ruby-1.9.3-p0 :006 > s.slice 3
 => nil 
ruby-1.9.3-p0 :007 > s.slice 3, 1
 => "" 
ruby-1.9.3-p0 :008 > s
 => "abc" 
ruby-1.9.3-p0 :009 > s.force_encoding 'ascii-8bit'
 => "abc" 
ruby-1.9.3-p0 :010 > s.slice 3
 => nil 
ruby-1.9.3-p0 :011 > s.slice 3, 1
 => "" 
ruby-1.9.3-p0 :012 > s
 => "abc" 
ruby-1.9.3-p0 :013 > s.valid_encoding?
 => true 
ruby-1.9.3-p0 :014 > s.force_encoding 'euc-jp'
 => "abc" 
ruby-1.9.3-p0 :015 > s.slice 3
 => nil 
ruby-1.9.3-p0 :016 > s.slice 3, 1
 => nil 
ruby-1.9.3-p0 :017 > s.valid_encoding?
 => true 

Thanks,
Brian


-- 
http://redmine.ruby-lang.org