Issue #8507 has been updated by stephencelis (Stephen Celis).


=begin
Hm... (({nil.to_h => {}})) is brand-new and creates an analog to (({nil.to_a})), so why would we remove (({nil.to_a}))?

(({(**nil)})) would be another tool that a programmer could use when handing arguments off between methods. One can currently use a single splat to convert nil or an empty array into an empty arg list. The ability to use double-splat to convert nil, an empty array, or an empty hash into a keyword arg list is equally powerful. It means flexibility in input and in handing off and converting arguments among different methods.

For instance, there was a recent moment where I thought I could elegantly double-splat an argument passed to a method in order to convert it to a hash without using (({kwargs || {}})), similar to what I've been able to do with a single splat and an args array (or, in some cases, a nil arg). The fact that I was unable to convert the object using a double-splat (while I can convert an object using a single-splat) seemed unpredictable. Rubyists have grown to expect that a single splat converts arguments passed in a certain way. I was hoping that handling the double-splat in a similar way would be an elegant way to resolve the current dissonance.

Here's a contrived example:

  def outer_method **options
    inner_method(**options[:inner_options])
  end

The ability to pass a value that may be a hash or may be nil as keyword arguments simplifies code (you don't have to check for nil) and makes it apparent as to what kind of value the inner method is taking (the same way a single splat denotes an args list).

I realize that keyword arguments are relatively new. I've already come up against some roadblocks using them (and finding their behavior fundamentally change between patchlevel 0 and 195). I think it's a great time to refine how we expect them to behave.

Again, the unexpected difference in behavior:

  Array(nil) # => []
  nil.to_a   # => []
  [*nil]     # => []

  Hash(nil)  # => {}
  nil.to_h   # => {}
  {**nil}    # TypeError: no implicit conversion of nil into Hash
=end
----------------------------------------
Bug #8507: Keyword splat does not convert arg to Hash
https://bugs.ruby-lang.org/issues/8507#change-39847

Author: stephencelis (Stephen Celis)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: current: 2.1.0
ruby -v: ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


=begin
A single splat is useful to convert an object to an array as it passes from one method to the next, so I expected the double-splat to do the same and convert the object to a hash.

  def splat *args
    p args
  end
  def double_splat **kwargs
    p args
  end
  splat(*nil)  # []
  splat(**nil) # TypeError: no implicit conversion of nil into Hash

For the sake of consistency, wouldn't it make sense to wrap the double-splatted arg with (({Hash()})) the same way a single-splatted arg is wrapped with (({Array()}))?
=end



-- 
http://bugs.ruby-lang.org/