Description
The io-extra library provides a few extra IO methods that you may find
handy. They are IO.closefrom
, IO.fdwalk
, IO.pread
, IO.pwrite
,
IO.writev
, IO#directio?
and IO#directio=
.
This library is not supported on MS Windows.
Support for OS X is limited. See below for details.
Installation
gem install io-extra
Adding the trusted cert
gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/io-extra/main/certs/djberg96_pub.pem)
Synopsis
require 'io-extra' # Do not use 'io/extra'
# Close all file descriptors from 3 up.
IO.closefrom(3)
# Inspect all the open handles
IO.fdwalk(0){ |handle|
puts "=" * 40
p handle
p handle.fileno
}
# Write an array to an IO object efficiently
IO.writev(fh.fileno, %w[a b c])
Developer's Notes
The "require 'io-extra'" is preferred over 'io/extra' because this is a mix of pure Ruby and C extension. The former require's the latter, so that way you get all the methods and constants that you expect.
You might be wondering what the difference is between my implementation of
IO.closefrom
and a pure Ruby version that looks something like this:
def IO.closefrom(n)
0.upto(n) do |fd|
IO.for_fd(fd).close
end
end
The primary difference is that this walks all file descriptors, rather
than only open file descriptors. However, I should note that this only
applies if your platform supports the closefrom()
function. In that case,
the only advantage is speed.
You might also be wondering what the difference is between my implementation of IO.fdwalk and a pure Ruby version that looks something like this:
def IO.fdwalk(n)
ObjectSpace.each_object(File){ |f|
yield f if f.fileno >= n
}
end
The primary difference is that this only closes Ruby file objects, not
necessarily every file handle opened by the Ruby process. For example,
handles opened via system()
calls.
Note to OS X Users
The OS X platform does not support closefrom()
or fdwalk()
. The hand-
crafted IO.closefrom
function will not work because the getrlimit()
function on OS X does not work. Patches welcome.
Update regarding pread and pwrite
As of Ruby 2.5, the IO#pread
and IO#pwrite
instance methods have been
baked into the language. Consequently, the singleton methods that this
library originally provided are now just wrappers around those instance
methods.
Documentation
For further documentation, see the io_extra.txt file or the inline documentation that was generated by RDoc (if you did a gem install).
Known Issues
The IO.writev
tests fail on Solaris. Short test scripts seem to work,
however. We're not sure what the issue is yet. Help wanted.
Update: As of 2018 Solaris is basically dead, so I'm not going to worry about supporting Solaris unless there's an outcry.
Please file any bug reports on the project page at https://github.com/djberg96/io-extra
Acknowledgements
Eric Wong for some great work on Linux compatibility and other fixes, as well as the code for the IO.writev method.
License
Apache-2.0
Copyright
(C) 2003-2021 Daniel J. Berger All Rights Reserved
Warranty
This package is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantability and fitness for a particular purpose.
Author
Daniel J. Berger