Bug #9733

Rails' HashWithIndifferentAccess is incompatible with Ruby's keyword arguments

Added by Konstantinos Karachalios over 1 year ago. Updated over 1 year ago.

[ruby-core:<unknown>]
Status:Rejected
Priority:Normal
Assignee:-
ruby -v:2.1.1 Backport:2.0.0: UNKNOWN, 2.1: UNKNOWN

Description

Origin: https://github.com/rails/rails/issues/14643

In Ruby it is possible to use a POR Hash in place of keyword arguments, making the transition between using an options = {} to keyword arguments easy.

The same is not true for HashWithIndifferentAccess and this makes it difficult to pass the params hash directly to methods with keyword arguments.

Bug test

require 'active_support/hash_with_indifferent_access'
require 'minitest/autorun'

def sum(a: 0, b: 0)
  a + b
end

class BugTest < MiniTest::Unit::TestCase
  def setup
    @normal_hash = {a: 18, b: 24} # just a hash
    @indiff_hash = ActiveSupport::HashWithIndifferentAccess.new(@normal_hash)
  end

  def test_ruby_hash
    assert_equal 42, sum(@normal_hash)
  end

  def test_hash_with_indifferent_access
    assert_equal 42, sum(@indiff_hash)
  end
end

Workaround

sum(@indiff_args.symbolize_keys)

VERSIONS

  • ruby 2.0.0-p353 & activesupport 4.0.1
  • ruby 2.1.0-p0 & activesupport 4.0.3
  • ruby 2.1.1-p76 & activesupport 4.0.4, 3.2.17

Related issues

Duplicates Ruby trunk - Bug #9731: Rails' HashWithIndifferentAccess is incompatible with Ruby's keyword arguments Open 04/11/2014

History

#1 Updated by Konstantinos Karachalios over 1 year ago

Accidental duplicate of https://bugs.ruby-lang.org/issues/9731 because server was timing out its response.

#2 Updated by Konstantinos Karachalios over 1 year ago

Accidental duplicate of https://bugs.ruby-lang.org/issues/9731 because of server timing out its response.

#3 Updated by Konstantinos Karachalios over 1 year ago

Accidental duplicate of https://bugs.ruby-lang.org/issues/9731 because of server timing out its response.

#4 Updated by Yusuke Endoh over 1 year ago

  • Duplicates Bug #9731: Rails' HashWithIndifferentAccess is incompatible with Ruby's keyword arguments added

#5 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Open to Rejected

Also available in: Atom PDF