py2rb icon indicating copy to clipboard operation
py2rb copied to clipboard

byte literals not supported

Open jayvdb opened this issue 4 years ago • 4 comments

b'a' are emitted as is, causes a syntax error in the ruby

jayvdb avatar Jul 06 '21 12:07 jayvdb

@jayvdb any thought on what Ruby construct this would map to?

nanobowers avatar Jul 07 '21 02:07 nanobowers

https://stackoverflow.com/a/15843685/5037965 has three options. IMO Array.pack should be the default approach used here, with String approaches only used if the literal looks like usable ascii text.

jayvdb avatar Jul 07 '21 03:07 jayvdb

There's probably a lot to more to this than just literal support - it's just the tip of the iceberg, so to speak. python byte objects seem to look like either an array of integers or a string depending on the context:

>>> a = b'abcd@\xef\xdd'
>>> a
b'abcd@\xef\xdd'
>>> print(a)
b'abcd@\xef\xdd'
>>> a[0]
97
>>> a[1]
98
>>> a[1:3]
b'bc'
>>> a[1] + a[2]
197

Using pack/unpack in ruby would either store the data as an array of integers or an encoded string, but if stored as a ruby String or Array, then getting the above functionality may be impractical. Depending on how well supported this needs to be, one could write PyBytes and PyByteArray classes that store the data internally as either a string or Array (optionally frozen in the case of bytes()) and emulate the methods here: https://docs.python.org/3.8/library/stdtypes.html#bytes-and-bytearray-operations

nanobowers avatar Jul 10 '21 20:07 nanobowers

Sounds like bytes is likely to be a lot of work. I personally am not using bytes in the projects I transpile. I just noticed it and raised the issue so it is easier for others to evaluate whether py2rb will meet their needs.

jayvdb avatar Jul 10 '21 23:07 jayvdb