Pack the contents of arr into a binary sequence in Crystal

扶醉桌前 提交于 2019-12-12 16:06:11

问题


Is there a standard way to achieve the same result as in Ruby with Array#pack method:

[1,2].pack­ "LL"
=> "\x01\x00\x00\x00\x02\x00\x00\x00"

回答1:


Not yet, and most likely, it wont be. The reason is that usually the result of pack is used to be sent to an IO (do you have any other case in mind?), so instead of allocating the result in memory, we're thinking about providing equivalent methods in the IO itself to send the data directly to the socket, file, etc...

It's not written in stone and still in the design board, but avoiding unnecessary intermediate objects in memory is one of the design principles in Crystal.



来源:https://stackoverflow.com/questions/32725506/pack-the-contents-of-arr-into-a-binary-sequence-in-crystal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!