Skip to content

Commit 2f8100c

Browse files
committed
[GR-16394] Fix Array#to_h.
PullRequest: truffleruby/886
2 parents a986766 + d5bf3c1 commit 2f8100c

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Bug fixes:
44

55
* Sharing for thread-safety of objects is now triggered later as intended, e.g., when a second `Thread` is started.
6+
* Fixed `Array#to_h` so it doesn't set a default value (#1698).
67

78
Compatibility:
89

spec/ruby/core/array/to_h_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
lambda { [].to_h(:a, :b) }.should raise_error(ArgumentError)
3636
end
3737

38+
it "produces a hash that returns nil for a missing element" do
39+
[[:a, 1], [:b, 2]].to_h[:c].should be_nil
40+
end
41+
3842
ruby_version_is "2.6" do
3943
context "with block" do
4044
it "converts [key, value] pairs returned by the block to a Hash" do

src/main/ruby/truffleruby/core/array.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ def to_ary
11821182
end
11831183

11841184
def to_h
1185-
h = Hash.new(size)
1185+
h = Hash.new
11861186
each_with_index do |elem, i|
11871187
elem = yield(elem) if block_given?
11881188
unless elem.respond_to?(:to_ary)

0 commit comments

Comments
 (0)