https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17097754782013-05-29T10:23:15ZRuby Issue Tracking SystemRuby master - Bug #8457: Function arguments: Is this intended?https://bugs.ruby-lang.org/issues/8457?journal_id=395502013-05-29T10:23:15Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>I am not sure what you meant, but I am sure you are fooled by side-effect of #concat method.</p>
<p>a = [1, 2]<br>
a.tap {|*p| # p is an array that wraps a i.e. p=[a]<br>
a.clear # a is cleared; now p=[[]]<br>
a.concat p # you concat p, that contains reference to a to a, make it circular<br>
} # => a = [a]</p>
<p>This is not a bug.</p> Ruby master - Bug #8457: Function arguments: Is this intended?https://bugs.ruby-lang.org/issues/8457?journal_id=395532013-05-29T10:54:01Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>=begin<br>
boris_stitnicky (Boris Stitnicky) wrote:</p>
<blockquote>
<p>a = [1, 2, x: 3]<br>
a.tap { |*p, q| a.clear.concat p } #=> [1, 2]</p>
<p>but</p>
<p>a = [1, 2, x: 3]<br>
a.tap { |*p, **q| a.clear.concat p } #=> [[...]]</p>
<p>and also</p>
<p>a = [1, 2]<br>
a.tap { |*p| a.clear.concat p } #=> [[...]]</p>
<p>???</p>
</blockquote>
<p>Is this the problem?</p>
<p>a = [1,2,x:3]<br>
a.tap { |*p,**q| puts "p=#{p.inspect}, q=#{q.inspect}" }</p>
<a name="prints-p1-2-xgt3-q"></a>
<h1 >prints: p=[[1, 2, {:x=>3}]], q={}<a href="#prints-p1-2-xgt3-q" class="wiki-anchor">¶</a></h1>
<a name="expected-p1-2-qxgt3"></a>
<h1 >expected: p=[1, 2], q={:x=>3}<a href="#expected-p1-2-qxgt3" class="wiki-anchor">¶</a></h1>
<p>as per:</p>
<p>a = [1,2,x:3]<br>
def foo(*p,**q) puts "p=#{p.inspect}, q=#{q.inspect}"; end<br>
foo *a</p>
<a name="prints-p1-2-qxgt3"></a>
<h1 >prints: p=[1, 2], q={:x=>3}<a href="#prints-p1-2-qxgt3" class="wiki-anchor">¶</a></h1>
<p>?</p>
<p>Note that the same behaviour occurs if the block has a (({**})) parameter.<br>
(Tried on ruby 2.1.0dev (2013-04-24 trunk 40439) [x86_64-linux] )<br>
=end</p> Ruby master - Bug #8457: Function arguments: Is this intended?https://bugs.ruby-lang.org/issues/8457?journal_id=395592013-05-30T00:58:25Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>I'm wondering too if there isn't something strange?</p>
<p>I'd expect a proc to either do an implicit splat or not, but right now it looks for options before doing the implicit splat. Should it not do it after doing the implicit splat?</p>
<p>I thought that when a proc had an argument list with more than one element, it was the same to call it with a single array argument than with the same array splatted:</p>
<p>Proc{|a, ...| ... }.call([...]) == Proc{|a, ...}| ... }.call(*[...]) # => Because of implicit splat</p>
<p>But we have currently:</p>
<p>Proc.new{|a, *b, **c| p a, b, c}.call(1,2, bar: 3)</p>
<a name="gt-1-2-bargt3-OK"></a>
<h1 >=> 1, [2], {:bar=>3} : OK<a href="#gt-1-2-bargt3-OK" class="wiki-anchor">¶</a></h1>
<p>Proc.new{|a, *b, **c| p a, b, c}.call([1,2, bar: 3])</p>
<a name="gt-1-2-bargt3-Expected-same-as-above"></a>
<h1 >=> 1, [2, {:bar=>3}], {}: Expected same as above<a href="#gt-1-2-bargt3-Expected-same-as-above" class="wiki-anchor">¶</a></h1>
<p>Proc.new{|(a, *b), **c| p a, b, c}.call([1,2], bar: 3)</p>
<a name="gt-1-2-bargt3-OK-2"></a>
<h1 >=> 1, [2], {:bar=>3} : OK<a href="#gt-1-2-bargt3-OK-2" class="wiki-anchor">¶</a></h1>
<p>Proc.new{|(a, *b), **c| p a, b, c}.call([[1,2], bar: 3])</p>
<a name="gt-1-2-bargt3-Expected-same-as-above-2"></a>
<h1 >=> [1, 2], [{:bar=>3}], {}: Expected same as above<a href="#gt-1-2-bargt3-Expected-same-as-above-2" class="wiki-anchor">¶</a></h1>
<p>So, Matz, what do you think of these simplified examples?</p> Ruby master - Bug #8457: Function arguments: Is this intended?https://bugs.ruby-lang.org/issues/8457?journal_id=395602013-05-30T01:32:05Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>As an additional note, this affects some methods of Enumerable when yielding multiple arguments.</p>
<p>For example:</p>
<pre><code>def each; yield 1, 2, bar: 3; end
include Enumerable
each{|a, *b, **c| p a, b, c} # => 1, [2], {:bar => 3}: ok
detect{|a, *b, **c| p a, b, c} # => 1, [2, {:bar => 3}], {}: should be the same, no?
</code></pre> Ruby master - Bug #8457: Function arguments: Is this intended?https://bugs.ruby-lang.org/issues/8457?journal_id=395622013-05-30T03:40:14Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>I admit there's a bug which Matthew mentioned, but it's not described in the OP.<br>
Do you mind if I close this, and ask you to resubmit as a new bug report for the record.</p>
<p>Matz.</p> Ruby master - Bug #8457: Function arguments: Is this intended?https://bugs.ruby-lang.org/issues/8457?journal_id=395632013-05-30T03:57:53Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Thanks.<br>
I created <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Proc auto-splat bug with named arguments (Closed)" href="https://bugs.ruby-lang.org/issues/8463">#8463</a></p>
<p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>I admit there's a bug which Matthew mentioned, but it's not described in the OP.<br>
Do you mind if I close this, and ask you to resubmit as a new bug report for the record.</p>
<p>Matz.</p>
</blockquote>