Ruby 2.6 の変更点 - Net, ERB

Ruby2.6アドベントカレンダーの23日目の記事です。

qiita.com

Net

Net::HTTPwrite_timeout 追加

https://bugs.ruby-lang.org/issues/13396

Net::HTTP.new 時に :write_timeout キーワード引数で設定したり、 Net::HTTP#write_timeout, Net::HTTP#write_timeout=, で参照したり設定したりできます。

デフォルトは60秒です。

Net::HTTPClientException が追加され、Net::HTTPServerException は非推奨

https://bugs.ruby-lang.org/issues/14688

4xx 系のステータスはサーバーエラーじゃなくてクライアントエラーだってことで、名前が変更されたようです。 ただ、Net::HTTPServerException がいきなり無くなると影響が大きいので、Net::HTTPClientExceptionNet::HTTPServerException の別名として定義されてます。

Net::HTTPServerException を直接呼ぶと warning が出力されます。

require 'net/http'

Net::HTTPClientException
#=> Net::HTTPServerException

Net::HTTPServerException
#=> warning: constant Net::HTTPServerException is deprecated

ERB

ERB.new:trim_mode:eoutvar キーワード引数追加

https://bugs.ruby-lang.org/issues/14256

ERB.new の2番目以降の引数が定位置じゃなくてキーワード引数になりました。

2番目の引数が safe_level だったのですが、$SAFE の扱いの変更に伴い、これが廃止されます。 そうすると、3, 4番目の引数が困るのでキーワード引数になったという感じのようです。

キーワード引数じゃない引数を2個以上指定すると warning が出力されるようになります(-w 指定時)。

% ruby -w -rerb -e 'ERB.new("", 1, 0, "hoge")'
-e:1: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.
-e:1: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.
-e:1: warning: Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.

erb コマンドの -S オプションが非推奨

これも上と同じ理由です。