/ RAILS

Rails 6 ActiveSupport::HashWithIndifferentAccess fix

Rails 6 fixes without (alias of slice) method on ActiveSupport::HashWithIndifferent for sybmol arguments.

ActiveSupport::HashWithIndifferentAccess#except is aliased to without method. When symbol argument was sent to without method on ActiveSupport::HashWithIndifferent object, it used to return the same object. It did not consider symbol arguments to perform without operation.

Let’s take an example to demonstrate the use case.

hash = { a: 4, b: 6, c: 7 }.with_indifferent_access

Before Rails 6

Now, if we want to fetch a new hash without key value pair having key a.

hash.without :a
{
    "a" => 4,
    "b" => 6,
    "c" => 7
}

As we can see from the result above, the resultant hash has the key passed as a symbol argument to without method.

Whereas it works if we pass string argument as given below.

hash.without "a"
{
    "b" => 6,
    "c" => 7
}

After Rails 6

Rails 6 has fixed the bug describe above. Now, if we try to perform the same operation, it gives the expected result with a hash having key-value pair having key a omitted from the result.

hash.without :a
{
    "b" => 6,
    "c" => 7
}

It also works with string argument to without method on ActiveSupport::HashWithIndifferentAccess.

hash.without "a"
{
    "b" => 6,
    "c" => 7
}

All this works as shows above with except method as well.

Reference