From: Kristof Bastiaensen Date: 2004-09-08T22:50:51+09:00 Subject: Re: ruby-dev summary 24171-24235 Hi, On Wed, 08 Sep 2004 18:29:22 +0900, Minero Aoki wrote: > [ruby-dev:24203] $~ alternative > > Shugo Maeda proposed a new multiple assignment scheme to provide an > alternative of $~ variable. His proposal is similar to the one of > Common Lisp: > > a = 1, 2, 3 > p a #=> 1 > > *a = 1, 2, 3 > p a #=> [1, 2, 3] > > This rule allows to let methods return additional values without > breaking backward compatibility. > > idx = "foo bar".index(/oo/) # compatible with current code idx, m > = "foo bar".index(/oo/) # m = $~ > > "foo bar".gsub(/oo/) do |str| # compatible with current code > .... > end > "foo bar".gsub(/oo/) do |str, m| # m = $~ > .... > end > > On the other hand, this scheme breaks codes which use `auto composite' > feature of multiple assignment: > > h = {1=>3, 2=>9, 3=>27} > h.each do |pair| > p pair #=> [1,3] now, but 1 by this proposal > end Interesting. A new multiple assignment scheme will break old code anyway. In this case you could write "h.each do |*pair|" instead. A bigger problem would be with the following: [[1, 3], [2, 9], [3, 27]].each do |arg1, arg2| p arg1.inspect #=> [1, 3] p arg2.inspect #=> nil end An idea would be to enable pattern matching of arrays inside argument lists and l-values (� la ML or prolog): [[1, 3], [2, 9], [3, 27]].each do |[arg1, arg2]| p arg1.inspect #=> 1 p arg2.inspect #=> 2 end Regards, KB