From: Louis-Philippe Date: 2008-12-13T04:25:52+09:00 Subject: Re: parallel method return value ------=_Part_15716_21119788.1229110377077 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi guys, Thanks for your answer,I get your points... but in this case, what I really want to do is have the method return the right number of values for the corresponding quantity of lvalues presented to it, to make it context sensitive you know, so I can have, for example, a boolean first return value when it is called with one lvalue and a boolean plus a string when called with two rvalue. The assignment operator does it, so I would have expected the same behaviour to also work with a method. right now my options are: a, = a_method or a = a_method[0] both ways still involve to ask for only one to return. What I am looking for, if it exist, is for the method to automatically detect context and act from it like the = operator. I find how the assignment operator implements this is very elegant, and am crave it for methods!! But... those are probably the only ways to do it, because as Brian said "methods always return exactly one value", and that is what I had previously concluded from experimentation and reading the documentation. Anybody can prove this wrong? (please!!) thanks! L-P 2008/12/12 Brian Candler > Louis-Philippe wrote: > > and with a method: > > > >>> def a_method > >>> return 1, 2, 3 > >>> end > > This behaves identically to > > return [1, 2, 3] > > That is, a single array object is returned. AFAIK, methods always return > exactly one value. > > > If I want only one lvalue, is this: > >>> a, = 1, 2, 3 > > => [1, 2, 3] > >>> a > > => 1 > > the only way to get it? > > a = a_method > > is saying "assign whatever object is returned from a_method to a", and > if that's an array, then an array is what you get. > > You can use the multiple-assignment syntax to drop the rest, or other > Array operations like > > a = a_method[0] > > a = a_method.first > > > I would have liked for the return behaviour to be set on the right side, > > to allow method invocation like: > >>> puts a_method > > > > to return: > > 1 > > > > instead of > > 1 > > 2 > > 3 > > If a function returns an array, that's what I want to see; I wouldn't > want it to be arbitarily munged. > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_15716_21119788.1229110377077--