From: Charles Mills Date: 2005-01-09T07:16:24+09:00 Subject: Re: multiple values & variable assignment Robert Klemme wrote: > "itsme213" schrieb im Newsbeitrag > news:WkJDd.9898$ho.9719@fe2.texas.rr.com... > > Why does > > def f; 1, 2, 3; end > > You sure, that this is the code you use? > > >> def f; 1, 2, 3; end > SyntaxError: compile error > (irb):49: syntax error > def f; 1, 2, 3; end > ^ > from (irb):49 > from (null):0 > > I'll use > > >> def f; return 1, 2, 3; end > => nil > > > x,y,z = f > > give x=1, y=2, z=3 > > > > While > > x = f > > gives x=[1, 2, 3]? > You can think of it as return splat. Looks like Ruby 2.0 will give you piece of mind, see below... > It's an optimization to save typing: if there are multiple values and on one > side there is only one value "*" is automatically prepended: > > >> x = f > => [1, 2, 3] > >> x > => [1, 2, 3] > >> x = *f > => [1, 2, 3] > >> x > => [1, 2, 3] > > If you want just the first element you got to do > > >> x, = f > => [1, 2, 3] > >> x > => 1 (...) I think this is the same issue as irb> def yields irb> yield(1,2) irb> end => nil irb> yields { |a| puts a.inspect } (irb):4: warning: multiple values for a block parameter (2 for 1) from (irb):2 [1, 2] => nil More info on this here: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/23568 I know there is an English version of that post somewhere, but I can't find it on google... -Charlie