Both IO and Float define a to_io method for writing the value to an io. The implementation trivially calls ByteFormat#encode, so num.to_io(io, format) is just equivalent to format.encode(num, io).
So this might already be considered a anvoidable aliases.
What's even more of a problem: The methods don't work with all Int and Float implementations. BigInt and BigFloat and fail:
BigInt.new.to_io(io, IO::ByteFormat::SystemEndian) # Error: no overload matches 'IO::ByteFormat::LittleEndian.encode' with types BigInt, IO::Memory
BigFloat.new.to_io(io, IO::ByteFormat::SystemEndian) # Error: no overload matches 'IO::ByteFormat::LittleEndian.encode' with types BigFloat, IO::Memory
I'm not sure if these methods have a special reason to exist, otherwise I'd probably just deprecate and remove them.
馃憤 for removal
Huge :-1: for removal - these methods exist because of IO#read_bytes and IO#write_byes which are the standard interfaces for reading and writing binary values from IO. These are like the IO#puts for binary - you just implement #to_io instead of #to_s.
Using IO::ByteFormat for ints is somewhat of an implementation detail, I think that this should become a real enum so that you can use :system_endian in the read/write_bytes calls.
The reason they exist is to support binary serialization. I can imagine a struct would implement to_io and from_io by writing all the byte values directly to the IO. Something like Ruby's pack and unpack but in a more structured, type-safe way.
If the issue is that BigInt and BigFloatdon't support those methods, let's move them to the primitive types.
I see. That's what I missed.
So we should probably just implement to_io (and from_io respectively) for Big* types.
LibGMP has import and export functions that could probably work for this: https://gmplib.org/manual/Integer-Import-and-Export.html#Integer-Import-and-Export Alternatively we could roll our own.
I'd be fine with implementing them or not.
After all, it's not common at all to see GMP types over a network protocol, which is primarily what these methods are used for implementing.
Yeah, probably not. Most data formats which would use such numbers probably use some kind of structured data and encode big numbers as strings.
Still, the current state needs fixing. The API defines Int#to_io but it doesn't work on BigInt. So either we make it work or move #to_io from Int to only those types where it is actually implemented.
In my mind, if it doesn't work then it's not defined.
But it's documented to work.