stringironruby

How to work around IronRuby packing bug?


Consider the following code (a small test-case boiled down from a real problem):

abc="ABC"
b="B"
print abc.index(b)
print "\n"

abc2=abc.unpack('C*').pack('C*')
b2=b.unpack('C*').pack('C*')
print abc2.index(b2)
print "\n"

In IdeOne it outputs

1
1

Run with IronRuby (32- or 64-bit) from the command-line it outputs

1
nil

And despite this, if I also print abc==abc2 it gives true.

This is clearly a bug in IronRuby, but it's a dead project so there's not much hope that it will be fixed in the near future.

Does anyone know a workaround which will allow me to pack ASCII back into strings which actually behave as one would expect?


Solution

  • I once ran into a similar issue and had to add a +'' to force string coercion.

    In the above code, the line

    b2=b.unpack('C*').pack('C*')
    

    should be replaced with

    b2=b.unpack('C*').pack('C*') + ''
    

    and it'll work as expected.