I want to pass an optional argument (any datatype, even a Hash) followed by a list of keyword arguments (which can be empty) to a method.
This is what I got:
def my_method(subject = nil, **options)
[subject, options]
end
The method output is as expected in the following cases:
nothing
my_method() # => [nil, {}]
a subject
my_method('foo') # => ["foo", {}]
a literal subject with options
my_method('foo', bar: 'bar') # => ["foo", {:bar=>"bar"}]
a
Hash
as subject with optionsmy_method({foo: 'foo'}, bar: 'bar') # => [{:foo=>"foo"}, {:bar=>"bar"}]
no subject, only options
my_method(bar: 'bar') # => [nil, {:bar=>"bar"}]
When passing a Hash
as subject and no options, the desired outcome is:
my_method({foo: 'foo'})
# => [{:foo=>"foo"}, {}]
But I get the following; I don't get the correct subject:
my_method({foo: 'foo'})
# => [nil, {:foo=>"foo"}]
Is my_method(foo: 'foo')
equivalent to my_method({foo: 'foo'})
? Any ideas on how I could get the desired outcome?