Ruby 2.4 の変更内容 その3 - 標準添付ライブラリ

Ruby 2.4の変更内容の標準添付ライブラリ編です。



cgi

「,」をクッキーのセパレータとして許さない

RFC2965 では「;」と「,」が許されてましたが、RFC6265 では「;」だけになったようです。

csv

liberal_parsing オプションが追加

RFC 4180 に従わないようなCSVでもパースできます。

require 'csv'

CSV.parse('abc"def')  #=> Illegal quoting in line 1. (CSV::MalformedCSVError)
CSV.parse('abc"def', liberal_parsing: true)  #=> [["abc\"def"]]

date

DateTime#to_time がタイムゾーンを保持する

require 'date'

dt = DateTime.new(2018, 1, 22, 12, 34, 56)
dt.to_s     #=> "2018-01-22T12:34:56+00:00"

# Ruby 2.3
dt.to_time  #=> 2018-01-22 21:34:56 +0900

# Ruby 2.4
dt.to_time  #=> 2018-01-22 12:34:56 +0000

ipaddr

IPAddr#== と IPAddr#<=> が例外を発生しない

require 'ipaddr'

IPAddr.new("1.2.3.4") == "hoge"  #=> false (2.3 では invalid address (IPAddr::InvalidAddressError))

irb

Binding#irb 追加

プログラム内に binding.irb と書いておくと、実行時にそこで irb が起動します。

a = 123
binding.irb
% ruby a.rb

From: a.rb @ line 2 :

    1: a = 123
 => 2: binding.irb

irb(main):001:0> a
=> 123
irb(main):002:0>

logger

Logger.new にキーワード引数 level, progname, formatter,datetime_format, shift_period_suffix 追加

同名のメソッドで設定できるパラメータをインスタンス作成時に指定できるようになりました。

Logger#shift_period_suffix 追加

ログファイル切り替え時のファイル名のサフィックスを指定できます。

net/http

Net::HTTP.post 追加

require 'net/http'
require 'uri'

Net::HTTP.post URI('http://www.example.com'), '{"abc":123}', 'Content-Type'=>'application/json'

net/ftp

TLS サポート

Net::FTP.new でキーワード引数でパラメータを指定可能

Net::FTP#status にオプション引数 pathname 追加

openssl

Ruby/OpenSSL 2.0

詳しくは https://github.com/ruby/openssl/blob/master/History.md#version-200

optparse

OptionParser#parse にキーワード引数 into 追加

オプション毎に #on メソッドのブロックで値を処理しなくても、#parse に into で Hash オブジェクトを渡せば、自動的に値が入るようになりました。便利!

require 'optparse'

opts = OptionParser.new
opts.on('-a')
opts.on('-x=#')
config = {}
opts.parse(["-ax123"], into: config)
config  #=> {:a=>true, :x=>"123"}

pathname

Pathname#empty? 追加

ファイル/ディレクトリが空であれば真を返します。

psych

Psych 2.2.2

rdoc

RDoc 5.0.0

readline

Readline.quoting_detection_proc, Readline.quoting_detection_proc= 追加

詳しくないのでパス。

rexml

REXML::Element#[] に属性名を渡すと属性の値を取得できる

require 'rexml/document'

doc = REXML::Document.new("<a attr='1'><b/><c/></a>")
doc.root[1]       #=> <c/> (2番目の子要素)
doc.root['attr']  #=> '1'  (attrの値)

rubygems

RubyGems 2.6.8

set

Set#compare_by_identity, Set#compare_by_identity? 追加

要素の比較を object_id によって行うようになります。

require 'set'

set = Set.new(['a'])
set << 'a'
set  #=> #<Set: {"a"}>
set.compare_by_identity
set << 'a'
set  #=> #<Set: {"a"}>

shellwords

Shellwords.shellwords のエスケープ文字 \

「\」がエスケープ文字として働くのは「$」「`」「"」「\」 改行の前だけになりました。

require 'shellwords'

s = <<'EOS'
printf "%s\n" hoge
EOS

# Ruby 2.3
Shellwords.shellwords s  #=> ["printf", "%sn", "hoge"]

# Ruby 2.4
Shellwords.shellwords s  #=> ["printf", "%s\\n", "hoge"]

time

Time#to_time がタイムゾーンを保持する

require 'time'
t = Time.new(2018,1,22,12,34,56,"+01:00")

# Ruby 2.3
t.to_time  #=> 2018-01-22 20:34:56 +0900

# Ruby 2.4
t.to_time  #=> 2018-01-22 12:34:56 +0100

tk

標準ライブラリから削除

必要な場合は GitHub にあります https://github.com/ruby/tk

webrick

「,」をクッキーのセパレータとして許さない

cgiと同じです。

xmlrpc

標準ライブラリから削除

ただし bundled gemとしては残ってるので、今までどおり使えます。

zlib

Zlib.gzip, Zlib.gunzip 追加

require 'zlib'

Zlib.gzip("hogehoge")
#=> "\x1F\x8B\b\x00T\xE5eZ\x00\x03\xCB\xC8OO\xCD\x00b\x00\xF2\xA4\x8C\xF5\b\x00\x00\x00"

Zlib.gunzip(Zlib.gzip("hogehoge"))
#=> "hogehoge"