I'm guessing that buffer
is binary encoded (i.e. buffer.encoding
is ASCII-8BIT). That means that this is happening:
c = @receiver.getbyte # c also has binary encoding
ff = "\xff" # ff is probably UTF-8
c.eql? ff # comparing strings in different encodings gives you `false`
Instead, you should be doing simple numeric comparisons:
@receiver.getbyte.getbyte(0) == 255
Or even better, modify getbyte
to return an integer.
I think your 1.9.X version was working mostly by accident, you should have been using a simple numeric comparison all along.
Keep in mind that you have bytes in your buffer (such as 0xFF and 0x00) that are not valid in UTF-8 strings so you should keep your buffer in binary. You can use force_encoding
on slices of the buffer for the parts that are UTF-8 strings. You might want to look at String#unpack
as well.
Note that I'm linking to the Ruby 2.0.0 specific docs to match the Ruby version you're currently using.