There is an alternative to the magic number -1 in ruby?

Hi.

In Ruby’s documentation I find this code to write in a file, the number 20 indicates that it must be written from byte number 20:

IO.binwrite("Test.txt", "0123456789", 20)

Okay, but I want to do something like this:

IO.binwrite("Test.txt", "0123456789", -1)

I mean, I want to start writing from the last byte, I’ve tried, and of course, it’s wrong.

An idea that occurs to me is to measure the size of the file in bytes and specify it with a variable:

file_size = File.size("Test.txt")
IO.binwrite("Test.txt", "0123456789", file_size)

But is that the only way I can do it?

Is there some reason this won’t work for you?

File.open('Test.txt', 'a') {|f| f.write('0123456789')}

My mistake, I didn’t know that .write also wrote in binary (which is what I needed).

This is how I can use it:

binary = ‘0001000011’
File.open(‘TestBin.txt’, ‘a’) {|f| f.write([binary].pack(‘B*’))}

Anyway, it’s not bad to know. Doesn’t the magic number -1 exist in Ruby?