Project

General

Profile

Bug #9291

array splatting a nil works, but hash splatting a nil does not

Added by First Last about 2 years ago. Updated over 1 year ago.

Status:
Open
Priority:
Normal
ruby -v:
ruby 2.0.0p353 (2013-11-22 revision 43784) [i686-linux]
Backport:
1.9.3: UNKNOWN, 2.0.0: UNKNOWN, 2.1: UNKNOWN
[ruby-core:59297]

Description

irb(main):001:0> [nil]
=> []
irb(main):002:0> {
*nil}
TypeError: no implicit conversion of nil into Hash

History

#1 [ruby-core:59347] Updated by First Last about 2 years ago

this could be used like this: method a: 1, **({b: 1} if condition)

#2 [ruby-core:64191] Updated by First Last over 1 year ago

So is there a reason * and ** should work differently for nil?

#3 [ruby-core:64296] Updated by Marc-Andre Lafortune over 1 year ago

  • Assignee set to Yukihiro Matsumoto

Currently, * uses explicit conversion to array (to_a, not to_ary), while ** uses implicit conversion to hash (to_hash, not to_h).

So your question about nil can be explained this way, since nil.to_a #=> [] but nil.to_hash is not defined.

The real question is then: shouldn't ** be using explicit conversion to hash with to_h?

Also available in: Atom PDF