renameat2(2) introduced in linux kernel 3.15 takes following flags.
RENAME_EXCHANGE: Atomically exchange oldpath and newpath.
RENAME_NOREPLACE: Don't overwrite newpath of the rename. Return an error if newpath already exists.
This change makes File.rename take these flags as keyword arguments.
I think atomicity is important. If the kernel doesn't support atomic exchange, File.exchange should raise NotImplementedError and users can handle it properly.
beginFile.exchange("hoge","fuga")rescueNotImplementedError# exchange files in another wayend
I think it's hard to have platform-specific API extensions like this to be in-core. If atomicity is such an important feature (and I'm sure it is), it's hard to emulate this in pure-userland for people without OS that support it.
This should be implemented as a gem. That way, gem install can bail out purposefully for unsupported platforms. That should prevent hidden race condition that you want to avoid.