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) over 7 years ago. Updated about 7 years ago.

Status:
Closed
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

Updated by shyouhei (Shyouhei Urabe) over 7 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
Actions #2

Updated by shyouhei (Shyouhei Urabe) over 7 years ago

  • Is duplicate of Bug #12821: Object converted to Hash unexpectedly under certain method call added
Actions #3

Updated by nobu (Nobuyoshi Nakada) about 7 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) about 7 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) about 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) about 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0