Skip to content

Commit

Permalink
Merge pull request #2863 from maimux2x/patch-2
Browse files Browse the repository at this point in the history
Hash#shiftのハッシュが空かつデフォルト値が設定されている場合の説明とサンプルコードの実行結果が古かったのを修正
  • Loading branch information
ohai authored Jul 27, 2024
2 parents 4f5c0d9 + 5bb1ac6 commit 642f048
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions refm/api/src/_builtin/Hash
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,11 @@ p h1.compare_by_identity? #=> true

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

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

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

#@samplecode 例
h = {:ab => "some" , :cd => "all"}
Expand All @@ -599,6 +599,28 @@ p h2 #=> {}
p h2.shift #=> [{}, nil]
#@end

#@else

ハッシュが空の場合、デフォルト値に関わらず nil を返します。

#@samplecode 例
h = {:ab => "some" , :cd => "all"}
p h.shift #=> [:ab, "some"]
p h.shift #=> [:cd, "all"]
p h #=> {}
p h.shift #=> nil

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

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

#@end

@see [[m:Array#shift]]

--- replace(other) -> self
Expand Down

0 comments on commit 642f048

Please sign in to comment.