I’m looking for Crystal’s equivalent of Ruby’s
This thread explains why Crystal doesn’t have it, but I’m wondering if anything has been added since this time to maybe support doing it a different way.
=> [0, 0, 160, 64]
const buf = Buffer.allocUnsafe(4);
Alright! Thanks to some help from @straight-shoota if anyone else comes looking for answers, here we go:
numbers = 
io = IO::Memory.new
numbers.each do |n|
io.write_bytes n.to_f32, IO::ByteFormat::LittleEndian
#=> [0, 0, 160, 64]
n = [65, 66, 67]
n.pack("ccc") #=> "ABC"
String.build do |io|
n.each do |number|
end #=> "ABC"
Yes, it’s a bit more verbose, but if you take a look at the closed issue on Crystal, it’ll make a little sense as to why. For me, the
pack/unpack methods in ruby always seemed a little mysterious. Seeing that it’s just writing some bytes to some IO really clears it up for me.