Bug #12884
closedUsing a HashWithIndifferentAccess with a default value in a function with a keyword parameter converts it to a Hash.
Description
A strange bug that broke our production when migrating from 2.1.9 to 2.2.5 (still present in 2.3.1)
It involves HashWithIndifferentAccess from active support. The version used is 3.2.22.4 (against all ruby versions)
Here's a bit of code to reproduce it :
def test(params = HashWithIndifferentAccess.new, disrupter: nil)
params.class
end
x = HashWithIndifferentAccess.new
x[:foo] = 'bar'
test x
# 2.3.1 => Hash
# 2.2.5 => Hash
# 2.1.9 => HashWithIndifferentAccess
But this only happens if there is a keyword parameter AND a default value for your HashWithIndifferentAccess parameter :
Not using a keyword parameter doesn't change the type
def test(params = HashWithIndifferentAccess.new, disrupter = nil)
params.class
end
x = HashWithIndifferentAccess.new
x[:foo] = 'bar'
test x
# 2.3.1 => HashWithIndifferentAccess
# 2.2.5 => HashWithIndifferentAccess
# 2.1.9 => HashWithIndifferentAccess
Not using a default value don't change the type
def test(params, disrupter: nil)
params.class
end
x = HashWithIndifferentAccess.new
x[:foo] = 'bar'
test x
# 2.3.1 => HashWithIndifferentAccess
# 2.2.5 => HashWithIndifferentAccess
# 2.1.9 => HashWithIndifferentAccess
Updated by shyouhei (Shyouhei Urabe) about 8 years ago
(Just to be clear) It has nothing to do with HWIA. I can reproduce the situation without it.
class X < Hash; end
def test(params = X.new, disrupter: nil)
return params
end
x = X.new
x['foo'] = 'bar'
p test(x).class
Updated by shyouhei (Shyouhei Urabe) about 8 years ago
- Is duplicate of Bug #12821: Object converted to Hash unexpectedly under certain method call added
Updated by nobu (Nobuyoshi Nakada) almost 8 years ago
- Status changed from Open to Closed
Applied in changeset r57360.
class.c: non-keyword hash class
- class.c (rb_extract_keywords): keep the class of non-keyword
elements hash as the original. [ruby-core:77813] [Bug #12884]
Updated by naruse (Yui NARUSE) almost 8 years ago
- Backport changed from 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED to 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE
ruby_2_4 r57861 merged revision(s) 57360.
Updated by usa (Usaku NAKAMURA) over 7 years ago
- Backport changed from 2.1: DONTNEED, 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE to 2.1: DONTNEED, 2.2: DONE, 2.3: REQUIRED, 2.4: DONE
ruby_2_2 r58092 merged revision(s) 57360.
Updated by nagachika (Tomoyuki Chikanaga) over 7 years ago
- Backport changed from 2.1: DONTNEED, 2.2: DONE, 2.3: REQUIRED, 2.4: DONE to 2.1: DONTNEED, 2.2: DONE, 2.3: DONE, 2.4: DONE
ruby_2_3 r58167 merged revision(s) 57360.