Project

General

Profile

Actions

Feature #18950

open

Hash#slice fails to copy default block

Added by RubyBugs (A Nonymous) over 1 year ago. Updated over 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:109386]

Description

An intense weekend debugging session discovered the following root cause of a bug: Hash#slice returns a new Hash, which has no default block set, even if the source Hash did have a default block set.

Simplified code to reproduce:

# Default to an empty hash for all accessed keys
hash_with_default = Hash.new { |h, k| h[k] = {} } # => {}
hash_with_default[:a]                             # => {}
hash_with_default[:b]                             # => {}
hash_with_default                                 # => {:a=>{}, :b=>{}}

# Later, use Hash#slice
hash_sliced = hash_with_default.slice(:a, :b)     # => {:a=>{}, :b=>{}}

# Finally, access a new key
hash_sliced[:c]                                   # => nil

# Error -- that was expected to call the default block
raise "No default value" unless hash_sliced[:c] == {}
Actions

Also available in: Atom PDF

Like0
Like0Like0