[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

parallel method return value

Louis-Philippe

12/12/2008 3:43:00 AM

[Note: parts of this message were removed to make it a legal post.]

Hi all,
this is assignment behaviour:

>> a,b = 1,2,3
=> [1, 2, 3]
>> a
=> 1
>> b
=> 2

and with a method:

>> def a_method
>> return 1, 2, 3
>> end
=> nil
>> a,b = a_method
=> [1, 2, 3]
>> a
=> 1
>> b
=> 2

If I want only one lvalue, is this:
>> a, = 1, 2, 3
=> [1, 2, 3]
>> a
=> 1
the only way to get it?

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

Thanks all!

L-P

16 Answers

Matthew Moss

12/12/2008 4:51:00 AM

0

> and with a method:
>
>>> def a_method
>>> return 1, 2, 3
>>> end
> => nil
>>> a,b = a_method
> => [1, 2, 3]
>>> a
> => 1
>>> b
> => 2
>
> If I want only one lvalue, is this:
>>> a, = 1, 2, 3
> => [1, 2, 3]
>>> a
> => 1
> the only way to get it?
>
> 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


So... uh, why wouldn't you just return one value? That is:

def a_method
return 1
end

???

Perhaps, if it's an array you have, then just use method #first:

def a_method
return some_array.first
end


Pierre Pat

12/12/2008 5:13:00 AM

0

How about
def a_method
[1, 2, 3].reverse
end
--
Posted via http://www.ruby-....

Brian Candler

12/12/2008 8:43:00 AM

0

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-....

Louis-Philippe

12/12/2008 7:26:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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 <b.candler@pobox.com>

> 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-....
>
>

Matthew Moss

12/12/2008 7:40:00 PM

0


On Dec 12, 2008, at 1:25 PM, Louis-Philippe wrote:

> 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.

But as was pointed out, methods _always_ return one value, even if it
appears to return multiple values. They are lumped together into one
array.

> 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!!

There isn't any way for the method call to recognize the context in
which it is being called. You'd have to pass an argument, such as the
"number of return values" to return.

a, b = a_method(2)
a = a_method(1)

Which seems pointless, when you can instead do what you've already
identified as the proper technique:

a, = a_method
a, b = a_method
a, b, c = a_method
# etc.

> Anybody can prove this wrong? (please!!)

No, because what was provided before wasn't wrong.


Louis-Philippe

12/12/2008 8:22:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Thanks Matthew,

"No, because what was provided before wasn't wrong."

Yeah... I believe you, but would really like it to be else!

L-P

2008/12/12 Matthew Moss <matt@moss.name>

>
> On Dec 12, 2008, at 1:25 PM, Louis-Philippe wrote:
>
> 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.
>>
>
> But as was pointed out, methods _always_ return one value, even if it
> appears to return multiple values. They are lumped together into one array.
>
> 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!!
>>
>
> There isn't any way for the method call to recognize the context in which
> it is being called. You'd have to pass an argument, such as the "number of
> return values" to return.
>
> a, b = a_method(2)
> a = a_method(1)
>
> Which seems pointless, when you can instead do what you've already
> identified as the proper technique:
>
> a, = a_method
> a, b = a_method
> a, b, c = a_method
> # etc.
>
> Anybody can prove this wrong? (please!!)
>>
>
> No, because what was provided before wasn't wrong.
>
>
>

Matthew Moss

12/12/2008 8:58:00 PM

0


On Dec 12, 2008, at 2:21 PM, Louis-Philippe wrote:

> Thanks Matthew,
>
> "No, because what was provided before wasn't wrong."
>
> Yeah... I believe you, but would really like it to be else!


Think of it this way...

def foo
return 1, 2, 3
end

a = foo

Now, you want a to be 1, but Ruby would need the power to read minds
to accommodate that. See, because if I do the same, I want a to be [1,
2, 3]. How is foo supposed to figure that out? Or why should your want
be more/less preferable than my want?

Simply, the interpreter can't know. There has to be some syntax or
something that gives foo, or the assignment to a, that key information.

Telling foo would be passing an arg:

def foo(nargs)
return [1, 2, 3][0...nargs]
end

a = foo(1)

Any of the other examples come after foo is done, so:

def foo
return 1, 2, 3
end

a, = foo
a = foo[0]
a = foo.first

All three of those do the same... assign 1 to a. The latter two
request specifically the first item, while the first does parallel
assignment (of array components to particular variables... in this
case, only a). Since you seem interested in parallel assignment to a
variable number of variables, the first seems appropriate. Granted,
forgetting the comma might be easy, but there's no way for foo to know
that you want a = foo to return 1 and that someone else that does a =
foo wants [1, 2, 3].



Einar Magnús Boson

12/12/2008 9:32:00 PM

0


On 12.12.2008, at 20:58 , Matthew Moss wrote:

>
> On Dec 12, 2008, at 2:21 PM, Louis-Philippe wrote:
>
>> Thanks Matthew,
>>
>> "No, because what was provided before wasn't wrong."
>>
>> Yeah... I believe you, but would really like it to be else!
>
>
> Think of it this way...
>
> def foo
> return 1, 2, 3
> end
>
> a = foo
>
> Now, you want a to be 1, but Ruby would need the power to read minds
> to accommodate that. See, because if I do the same, I want a to be
> [1, 2, 3]. How is foo supposed to figure that out? Or why should
> your want be more/less preferable than my want?
>
> Simply, the interpreter can't know. There has to be some syntax or
> something that gives foo, or the assignment to a, that key
> information.
>
> Telling foo would be passing an arg:
>
> def foo(nargs)
> return [1, 2, 3][0...nargs]
> end
>
> a = foo(1)
>
> Any of the other examples come after foo is done, so:
>
> def foo
> return 1, 2, 3
> end
>
> a, = foo
> a = foo[0]
> a = foo.first
>
> All three of those do the same... assign 1 to a. The latter two
> request specifically the first item, while the first does parallel
> assignment (of array components to particular variables... in this
> case, only a). Since you seem interested in parallel assignment to a
> variable number of variables, the first seems appropriate. Granted,
> forgetting the comma might be easy, but there's no way for foo to
> know that you want a = foo to return 1 and that someone else that
> does a = foo wants [1, 2, 3].
>
>
>


He isn't asking ruby to read minds, he is asking about overloading on
expected number of return values like e.g. matlab does. But it is like
everyone is saying, Ruby doesn't do that, sorry.

einarmagnus




Ezra Zygmuntowicz

12/12/2008 9:48:00 PM

0

Hi~

On Dec 12, 2008, at 12:58 PM, Matthew Moss wrote:

>
> On Dec 12, 2008, at 2:21 PM, Louis-Philippe wrote:
>
>> Thanks Matthew,
>>
>> "No, because what was provided before wasn't wrong."
>>
>> Yeah... I believe you, but would really like it to be else!
>
>
> Think of it this way...
>
> def foo
> return 1, 2, 3
> end
>
> a = foo
>
> Now, you want a to be 1, but Ruby would need the power to read minds
> to accommodate that. See, because if I do the same, I want a to be
> [1, 2, 3]. How is foo supposed to figure that out? Or why should
> your want be more/less preferable than my want?
>
> Simply, the interpreter can't know. There has to be some syntax or
> something that gives foo, or the assignment to a, that key
> information.
>
> Telling foo would be passing an arg:
>
> def foo(nargs)
> return [1, 2, 3][0...nargs]
> end
>
> a = foo(1)
>
> Any of the other examples come after foo is done, so:
>
> def foo
> return 1, 2, 3
> end
>
> a, = foo
> a = foo[0]
> a = foo.first
>
> All three of those do the same... assign 1 to a. The latter two
> request specifically the first item, while the first does parallel
> assignment (of array components to particular variables... in this
> case, only a). Since you seem interested in parallel assignment to a
> variable number of variables, the first seems appropriate. Granted,
> forgetting the comma might be easy, but there's no way for foo to
> know that you want a = foo to return 1 and that someone else that
> does a = foo wants [1, 2, 3].



you can use the masign commas to get sort of what you want:

irb(main):001:0> def foo
irb(main):002:1> return 1,2,3
irb(main):003:1> end
=> nil
irb(main):004:0> a, = foo
=> [1, 2, 3]
irb(main):005:0> a
=> 1


Cheers-

Ezra Zygmuntowicz
ez@engineyard.com




Matthew Moss

12/12/2008 10:08:00 PM

0

>> a, = foo
>> a = foo[0]
>> a = foo.first
>>
>> All three of those do the same... assign 1 to a. The latter two
>> request specifically the first item, while the first does parallel
>> assignment (of array components to particular variables... in this
>> case, only a). Since you seem interested in parallel assignment to
>> a variable number of variables, the first seems appropriate.
>> Granted, forgetting the comma might be easy, but there's no way for
>> foo to know that you want a = foo to return 1 and that someone else
>> that does a = foo wants [1, 2, 3].
>
> you can use the masign commas to get sort of what you want:
>
> irb(main):001:0> def foo
> irb(main):002:1> return 1,2,3
> irb(main):003:1> end
> => nil
> irb(main):004:0> a, = foo
> => [1, 2, 3]
> irb(main):005:0> a
> => 1


You mean like my first example above? :D