Project

General

Profile

Actions

Bug #11993

closed

foo(hash) is handled like foo(**hash)

Added by sos4nt (Stefan Schüßler) over 8 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:72871]

Description

Given this method:

def foo(a = nil, b: nil)
  p a: a, b: b
end

I can pass keyword arguments to it and I can turn a hash into keyword arguments with the ** operator:

foo(b: 1)       #=> {:a=>nil, :b=>1}
foo(**{b: 1})   #=> {:a=>nil, :b=>1}

What baffles me is that a hash is also turned into keyword arguments without the ** operator:

foo({b: 1})     #=> {:a=>nil, :b=>1}

This looks like a flaw to me. I was expecting:

foo({b: 1})     #=> {:a=>{:b=>1}, :b=>nil}

Which would have resembled the way * works:

def bar(a = nil, b = nil)
  p a: a, b: b
end

bar(1, 2)       #=> {:a=>1, :b=>2}
bar(*[1, 2])    #=> {:a=>1, :b=>2}
bar([1, 2])     #=> {:a=>[1, 2], :b=>nil}

But currently, there doesn't seem to be a difference between foo(hash) and foo(**hash).

Is this behavior intended? If so, what's the rationale behind this decision?

Actions

Also available in: Atom PDF

Like0
Like0Like0