Project

General

Profile

Actions

Bug #20064

closed

Inconsistent behavior between array splat *nil and hash splat **nil

Added by zeke (Zeke Gabrielse) 5 months ago. Updated 4 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:115736]

Description

This has been discussed in #8507 and #9291 but both were closed because they lacked a clear use case.

I think the following code should work, showing a clear use case:

invitation = if params.key?(:inviter_id)
               { invitation_attributes: params.slice(:inviter_id) }
             end

User.create(
  email: 'john.doe@ruby.example',
  first_name: 'John',
  first_name: 'Doe',
  **invitation,
)

Per the previous discussions, this is because * uses explicit conversion to array (to_a, not to_ary), while ** uses implicit conversion to hash (to_hash, not to_h).

I find it confusing that you can splat nil into an array, but not nil into a hash. It would make sense for ** to use explicit conversion.

Actions

Also available in: Atom PDF

Like4
Like1Like0Like0Like0Like0