Skip to content

Commit 7428595

Browse files
Watson1978claude
andcommitted
IO: timeout / timeout= / wait_priority と IO::TimeoutError を追加 (Ruby 3.2)
いずれも Ruby 3.2 で追加されたもの。実機で確認し版分岐した。 IO#timeout 設定されている入出力タイムアウトの取得 IO#timeout= 入出力タイムアウトの設定 IO#wait_priority 優先データの読み込み待ち IO::TimeoutError タイムアウト時に発生する例外 (< IOError) トラッカーでは IO#timeout= だけが未収録扱いだったが、getter の IO#timeout も 未収録だった (timeout ライブラリの Kernel#timeout と名前が衝突して収録済みと 誤検出されていた)。getter/setter の対として両方追加した。 IO::TimeoutError は IO.md が front matter に include を持つため同居できない (multiple entities in a file with front matter relations になる)。 IO__Buffer 系の例外と同じく IO__TimeoutError.md として独立ファイルにし、 版の出し分けは front matter の since: "3.2" で行った。 IO#timeout の既定値が nil、設定すると値を返すこと、タイムアウト時に IO::TimeoutError が発生することを実機で確認した。wait_priority の引数と 返り値 (真、またはタイムアウト時 nil) も実機と rdoc で確認済み。 bitclust のデータベース生成を 3.1 / 3.2 / 3.3 / 4.0 で実行してエラーが出ないこと、 登録される項目が 0 / 4 / 4 / 4 件と 3.2 追加どおりになること、および IO#wait_readable などの参照リンクが解決することを確認済み。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 5bcc2e4 commit 7428595

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

manual/api/_builtin/IO.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,60 @@ p IO.new(IO.sysopen("/")).path # => "/"
24312431
p IO.new(IO.sysopen("/"), path: "foo").path # => "foo"
24322432
```
24332433

2434+
### def timeout -> Numeric | nil
2435+
2436+
self に設定されている入出力のタイムアウトを返します。
2437+
設定されていない場合は nil を返します。
2438+
2439+
タイムアウトの設定については [m:IO#timeout=] を参照してください。
2440+
2441+
```ruby title="例"
2442+
r, w = IO.pipe
2443+
p r.timeout # => nil
2444+
r.timeout = 0.5
2445+
p r.timeout # => 0.5
2446+
```
2447+
2448+
- **SEE** [m:IO#timeout=]
2449+
2450+
### def timeout=(numeric) -> Numeric
2451+
### def timeout=(nil) -> nil
2452+
2453+
self に入出力のタイムアウトを秒単位で設定します。
2454+
2455+
設定すると、可能な限りすべてのブロッキング操作にこのタイムアウトが適用されます。
2456+
操作が設定した時間を超えると [c:IO::TimeoutError] が発生します。
2457+
2458+
影響を受けるのは [m:IO#gets][m:IO#puts][m:IO#read][m:IO#write]
2459+
[m:IO#wait_readable][m:IO#wait_writable] などです
2460+
([c:Socket] のブロッキング操作にも影響します)。
2461+
2462+
- **param** `numeric` -- タイムアウトの秒数を数値で指定します。
2463+
nil を指定するとタイムアウトを解除します。
2464+
2465+
```ruby title="例"
2466+
r, w = IO.pipe
2467+
r.timeout = 0.1
2468+
2469+
r.read # ~> IO::TimeoutError
2470+
```
2471+
2472+
- **SEE** [m:IO#timeout], [c:IO::TimeoutError]
2473+
2474+
### def wait_priority(timeout = nil) -> bool | self | nil
2475+
2476+
self が優先データを受信して読み込み可能になるまで待ちます。
2477+
2478+
優先データ(緊急データ)は [m:Socket::Constants::MSG_OOB] フラグを用いて
2479+
送受信され、通常はストリーム型のソケットに限られます。
2480+
2481+
- **param** `timeout` -- タイムアウトを秒単位の数値で指定します。
2482+
nil を指定すると読み込み可能になるまで待ち続けます。
2483+
- **return** -- 読み込み可能になった場合は真を、timeout で指定した時間が経過した
2484+
場合は nil を返します。
2485+
2486+
- **SEE** [m:IO#wait_readable], [m:IO#wait_writable]
2487+
24342488
#@end
24352489

24362490
## Constants
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
library: _builtin
3+
since: "3.2"
4+
---
5+
# class IO::TimeoutError < IOError
6+
7+
入出力の操作が [m:IO#timeout=] で設定した時間を超えたときに発生します。
8+
9+
```ruby
10+
r, w = IO.pipe
11+
r.timeout = 0.1
12+
13+
r.read # ~> IO::TimeoutError
14+
```
15+
16+
- **SEE** [m:IO#timeout=], [m:IO#timeout]

0 commit comments

Comments
 (0)