Project

General

Profile

Actions

Bug #12884

closed

Using a HashWithIndifferentAccess with a default value in a function with a keyword parameter converts it to a Hash.

Added by mderelle (Mathieu Derelle) almost 6 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:77813]

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

Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #12821: Object converted to Hash unexpectedly under certain method callClosedActions
Actions

Also available in: Atom PDF