https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-04-24T07:44:20ZRuby Issue Tracking SystemRuby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=388492013-04-24T07:44:20ZTylerRick (Tyler Rick)tyler@tylerrick.com
<ul></ul><p>=begin<br>
<a href="http://jp.rubyist.net/magazine/?Ruby200SpecialEn-kwarg#f01" class="external">http://jp.rubyist.net/magazine/?Ruby200SpecialEn-kwarg#f01</a> said:</p>
<p>Be careful when passing hashes to methods with both variable length argument lists and keyword arguments.</p>
<p>but in this example, the argument list is ((<em>not</em>)) variable length.<br>
=end</p> Ruby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=388502013-04-24T07:52:10Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>TylerRick (Tyler Rick) wrote:</p>
<blockquote>
<p>I'm able to pass any other type of object to my first argument:</p>
<p>def foo(hash, opt: true)<br>
puts "hash: #{hash}, opt: #{opt.inspect}"<br>
end</p>
<p>foo 'a' # => hash: a, opt: true<br>
foo [{a:1}] # => hash: [{:a=>1}], opt: true<br>
foo [{a:1}], opt: false # => hash: [{:a=>1}], opt: false</p>
<p>But when I try to pass a hash, it raises an ArgumentError:</p>
<p>foo({a:1}) # Raises ArgumentError: unknown keyword: a</p>
<a name="Expected-behavior-hash-agt1-opt-true"></a>
<h1 >Expected behavior: hash: {:a=>1}, opt: true<a href="#Expected-behavior-hash-agt1-opt-true" class="wiki-anchor">¶</a></h1>
</blockquote>
<p>Additionally, calling foo({opt:1}) throws ArgumentError: wrong number of arguments (0 for 1)</p>
<p>This issue can be worked around by calling: foo({a:1},{}) , but it is unexpected.</p> Ruby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=388582013-04-24T14:58:18Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>mame (Yusuke Endoh)</i></li></ul> Ruby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=395652013-05-30T19:50:44Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>This issue was solved with changeset r40992.<br>
Pablo, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<hr>
<ul>
<li>
<p>vm_insnhelper.c (vm_callee_setup_keyword_arg,<br>
vm_callee_setup_arg_complex): consider a hash argument for keyword<br>
only when the number of arguments is more than the expected<br>
mandatory parameters. <a href="/issues/8040">[ruby-core:53199]</a> [ruby-trunk - Bug <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Unexpect behavior when using keyword arguments (Closed)" href="https://redmine.ruby-lang.org/issues/8040">#8040</a>]</p>
</li>
<li>
<p>test/ruby/test_keyword.rb: update a test for above.</p>
</li>
</ul> Ruby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=559882016-01-07T02:34:09Zozydingo (Andrew Schwartz)
<ul><li><strong>ruby -v</strong> changed from <i>ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]</i> to <i>ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13]</i></li></ul><p>This is unfortunately still an issue with default values in positional arguments:</p>
<p>2.2.2 > def foo(hash={}, opt: true); p hash; p opt; end<br>
=> :foo<br>
2.2.2 > foo({a: 1})<br>
ArgumentError: unknown keyword: a</p>
<p>Expected behavior is that foo can be called with a hash argument in the first position without needing to specify the optional keyword args.</p>
<p>Yusuke Endoh wrote:</p>
<blockquote>
<p>This issue was solved with changeset r40992.<br>
Pablo, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<hr>
<ul>
<li>
<p>vm_insnhelper.c (vm_callee_setup_keyword_arg,<br>
vm_callee_setup_arg_complex): consider a hash argument for keyword<br>
only when the number of arguments is more than the expected<br>
mandatory parameters. <a href="/issues/8040">[ruby-core:53199]</a> [ruby-trunk - Bug <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Unexpect behavior when using keyword arguments (Closed)" href="https://redmine.ruby-lang.org/issues/8040">#8040</a>]</p>
</li>
<li>
<p>test/ruby/test_keyword.rb: update a test for above.</p>
</li>
</ul>
</blockquote> Ruby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=568062016-01-30T20:20:40Zavit (Andrew Vit)andrew@avit.ca
<ul><li><strong>Backport</strong> deleted (<del><i>1.9.3: UNKNOWN, 2.0.0: UNKNOWN</i></del>)</li></ul><p>Andrew Schwartz wrote:</p>
<blockquote>
<p>This is unfortunately still an issue with default values in positional arguments</p>
</blockquote>
<p>See <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: Mixing kwargs with optional parameters changes way method parameters are parsed (Rejected)" href="https://redmine.ruby-lang.org/issues/11967">#11967</a> for the explanation.</p> Ruby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=683852017-12-14T07:23:51Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/14183">Feature #14183</a>: "Real" keyword argument</i> added</li></ul> Ruby master - Bug #8316: Can't pass hash to first positional argument; hash interpreted as keyword argumentshttps://redmine.ruby-lang.org/issues/8316?journal_id=724102018-06-06T01:49:16ZTylerRick (Tyler Rick)tyler@tylerrick.com
<ul></ul><p>ozydingo (Andrew Schwartz) wrote:</p>
<blockquote>
<p>This is unfortunately still an issue with default values in positional arguments:</p>
<p>2.2.2 > def foo(hash={}, opt: true); p hash; p opt; end<br>
=> :foo<br>
2.2.2 > foo({a: 1})<br>
ArgumentError: unknown keyword: a</p>
<p>Expected behavior is that foo can be called with a hash argument in the first position without needing to specify the optional keyword args.</p>
</blockquote>
<p>Ay! Just ran into this today.</p>
<p>Looks like this case is tracked in <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Optional argument treated as kwarg (Closed)" href="https://redmine.ruby-lang.org/issues/12717">#12717</a> and <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: Mixing kwargs with optional parameters changes way method parameters are parsed (Rejected)" href="https://redmine.ruby-lang.org/issues/11967">#11967</a> but won't be fixed until Ruby 3 (<a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: "Real" keyword argument (Closed)" href="https://redmine.ruby-lang.org/issues/14183">#14183</a>).</p>
<p>I wish there were a good workaround. Best I was able to come up with was:</p>
<pre><code>def foo(hash={}, options = {opt: true})
opt = options[:opt]
[hash, opt]
end
> foo({a: 1})
=> [{:a=>1}, true]
> foo({a: 1}, opt: false)
=> [{:a=>1}, false]
</code></pre>