Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash#shiftのハッシュが空かつデフォルト値が設定されている場合の説明とサンプルコードの実行結果が古かったのを修正 #2863

Merged
merged 3 commits into from
Jul 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions refm/api/src/_builtin/Hash
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,7 @@ p h1.compare_by_identity? #=> true

shiftは破壊的メソッドです。selfは要素を取り除かれた残りのハッシュに変更されます。

ハッシュが空の場合、デフォルト値([[m:Hash#default]]または[[m:Hash#default_proc]]のブロックの値か、どちらもnilならばnil)
を返します(このとき、[key,value] という形式の値を返すわけではないことに注意)。

将来のバージョン(Ruby 3.2を予定)ではデフォルト値に関わらず nil になる予定なので、デフォルト値を設定しているハッシュで
shift を使う場合は注意してください。([[bug:16908]])
ハッシュが空の場合、デフォルト値に関わらず nil を返します。

#@samplecode 例
h = {:ab => "some" , :cd => "all"}
Expand All @@ -592,11 +588,11 @@ p h.shift #=> nil

h1 = Hash.new("default value")
p h1 #=> {}
p h1.shift #=> "default value"
p h1.shift #=> nil

h2 = Hash.new {|*arg| arg}
p h2 #=> {}
p h2.shift #=> [{}, nil]
p h2.shift #=> nil
#@end

@see [[m:Array#shift]]
Expand Down