iTranslated by AI
Ruby 3.2 - IO / FileUtils
This is the article for day 8 of the Ruby 3.2 Advent Calendar.
IO
Addition of IO#timeout
You can now set a timeout with IO#timeout.
$stdin.timeout = 1
gets
#=> Blocking operation timed out! (IO::TimeoutError)
r, w = IO.pipe
w.timeout = 1
loop{w.write("hoge"*100)}
#=> Blocking operation timed out! (IO::TimeoutError)
It is convenient because you don't have to use things like Timeout.timeout anymore.
Using Timeout.timeout consumes a thread and is costly, and its exception handling is also quite specific.
IO#wait / IO#wait_readable / IO#wait_writable are now built-in
I noticed this while writing, but the IO#wait* methods have become built-in. It's not mentioned in the NEWS.
Until Ruby 3.1, you had to require 'io/wait'.
This is also quite convenient.
FileUtils
Addition of relative option to FileUtils.ln_s
FileUtils includes methods similar to Unix file manipulation commands.
FileUtils.ln_s creates symbolic links like the ln -s command.
require 'fileutils'
FileUtils.ln_s('/tmp/a/b/c', 'hoge')
puts File.readlink('hoge') #=> "/tmp/a/b/c"
The ln -s command usually uses the path specified in the first argument as is, but:
% cd /tmp/a/b
% ln -s /tmp/a/b/c hoge
% ls -l hoge
lrwxrwxrwx 1 tommy tommy 10 12月 6 22:06 hoge -> /tmp/a/b/c
When the -r option is added to ln -s, a relative path is used based on the link location.
% cd /tmp/a/b
% ln -sr /tmp/a/b/c hoge
% ls -l hoge
lrwxrwxrwx 1 tommy tommy 10 12月 6 22:06 hoge -> c
A relative option that behaves the same as -r has been added to FileUtils.ln_s.
require 'fileutils'
FileUtils.ln_s('/tmp/a/b/c', 'hoge', relative: true)
puts File.readlink('hoge') #=> "c"
The ln_sr method, which provides the same behavior, has also been added.
require 'fileutils'
FileUtils.ln_sr('/tmp/a/b/c', 'hoge')
puts File.readlink('hoge') #=> "c"
Discussion