# Multiple return and parallel assignement

**URL:** <https://rubytalk.org/t/multiple-return-and-parallel-assignement/18534>\
**Category:** ruby-talk\
**Created:** [20 May 2005 13:05 UTC](https://rubytalk.org/t/multiple-return-and-parallel-assignement/18534 "2005-05-20T13:05:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jean](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jean](https://rubytalk.org/u/jean)\
**Post date:** [20 May 2005 13:05 UTC](https://rubytalk.org/t/multiple-return-and-parallel-assignement/18534/1 "2005-05-20T13:05:15Z")

</div>

Hi,  
I'm studying Ruby and I'm glad to find similar behaviours of two common  
lisp operators that I like: parallel assignement has a similar behavior  
of the 'let' special operator while the multiple-return is similar to  
'values'.  
A little difference (syntax apart :-)) is that if in common lisp you  
make available only one variable to contains the return values of an  
expression it gets the first value returned and not an array with all  
the values as in ruby:  
An example may be useful, in lisp:  
CL-USER\> (defvar myvar)  
MYVAR  
CL-USER\> (defun mymethod () (values 1 2))  
MYMETHOD  
CL-USER\> (setf myvar (mymethod))  
1  
CL-USER\> myvar  
1  
CL-USER\>

In ruby:  
irb(main):003:0\> def mymethod  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1, 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
irb(main):005:1\> nil  
irb(main):006:0\> a = mymethod  
[1, 2]  
irb(main):007:0\> a  
[1, 2]  
irb(main):008:0\>

Thinking at the use I've found of the multiple return I find that  
perhaps the common lisp choice is more pragmatic beacause usually the  
multiple return is used to define method where in the 99% of cases is  
taken only the first element and in few cases the rest.  
In common lisp for example the gethash function for the hashtable  
returns 2 values, the value in the hash associated at the key and a  
boolean value that is true if the value returned is in the hashtable or  
false if is the default value. In most of cases this boolean is ignored  
and in the code you assign only the first returned value at a variable:  
CL-USER\> (setf myvar (gethash key my-hashtable))

What do you think about? (Perhaps I don't know enough Ruby and there is  
a way to do the same thing in Ruby 😉 )

Thinking at Ruby I'd like that if I call mymethod defined above with  
only a variable it gets the first value:

irb(main):006:0\> a = mymethod  
1  
irb(main):007:0\> a  
1  
irb(main):008:0\>

and if use a variable with \* then it gets an array with all the values:

irb(main):006:0\> \*a = mymethod  
[1, 2]  
irb(main):007:0\> a  
[1, 2]  
irb(main):008:0\>

What do you think about?

Thanks in advance.

Giannandrea

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [20 May 2005 13:13 UTC](https://rubytalk.org/t/multiple-return-and-parallel-assignement/18534/2 "2005-05-20T13:13:02Z")

</div>

Hi --

> **···**
>
> On Fri, 20 May 2005, jean wrote:
> 
> > Thinking at Ruby I'd like that if I call mymethod defined above with  
> > only a variable it gets the first value:
> > 
> > irb(main):006:0\> a = mymethod  
> > 1  
> > irb(main):007:0\> a  
> > 1  
> > irb(main):008:0\>
> > 
> > and if use a variable with \* then it gets an array with all the values:
> > 
> > irb(main):006:0\> \*a = mymethod  
> > [1, 2]  
> > irb(main):007:0\> a  
> > [1, 2]  
> > irb(main):008:0\>
> > 
> > What do you think about?
> 
> If you want just one value it's easy:
> 
> &nbsp;&nbsp;&nbsp;a, = mymethod
> 
> or
> 
> &nbsp;&nbsp;&nbsp;a = mymethod[0]
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [20 May 2005 13:16 UTC](https://rubytalk.org/t/multiple-return-and-parallel-assignement/18534/3 "2005-05-20T13:16:31Z")

</div>

Probably not what you want, but:

irb(main):001:0\> def mymethod  
irb(main):002:1\> return 1, 2  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> one = mymethod.first  
=\> 1

Hope that helps.

James Edward Gray II

> **···**
>
> On May 20, 2005, at 8:05 AM, jean wrote:
> 
> > What do you think about? (Perhaps I don't know enough Ruby and there is  
> > a way to do the same thing in Ruby 😉 )
> > 
> > Thinking at Ruby I'd like that if I call mymethod defined above with  
> > only a variable it gets the first value:

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [20 May 2005 13:22 UTC](https://rubytalk.org/t/multiple-return-and-parallel-assignement/18534/4 "2005-05-20T13:22:36Z")

</div>

You can have both. (Note the "," in line 003)

$ irb  
irb(main):001:0\> a = [1,2]  
=\> [1, 2]  
irb(main):002:0\> a  
=\> [1, 2]  
irb(main):003:0\> a, = [1,2]  
=\> [1, 2]  
irb(main):004:0\> a  
=\> 1

best regards,

Brian

> **···**
>
> On 20/05/05, jean \<g.castaldi@gmail.com\> wrote:
> 
> > Hi,  
> > I'm studying Ruby and I'm glad to find similar behaviours of two common  
> > lisp operators that I like: parallel assignement has a similar behavior  
> > of the 'let' special operator while the multiple-return is similar to  
> > 'values'.  
> > A little difference (syntax apart :-)) is that if in common lisp you  
> > make available only one variable to contains the return values of an  
> > expression it gets the first value returned and not an array with all  
> > the values as in ruby:  
> > An example may be useful, in lisp:  
> > CL-USER\> (defvar myvar)  
> > MYVAR  
> > CL-USER\> (defun mymethod () (values 1 2))  
> > MYMETHOD  
> > CL-USER\> (setf myvar (mymethod))  
> > 1  
> > CL-USER\> myvar  
> > 1  
> > CL-USER\>
> > 
> > In ruby:  
> > irb(main):003:0\> def mymethod  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1, 2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > irb(main):005:1\> nil  
> > irb(main):006:0\> a = mymethod  
> > [1, 2]  
> > irb(main):007:0\> a  
> > [1, 2]  
> > irb(main):008:0\>
> > 
> > Thinking at the use I've found of the multiple return I find that  
> > perhaps the common lisp choice is more pragmatic beacause usually the  
> > multiple return is used to define method where in the 99% of cases is  
> > taken only the first element and in few cases the rest.  
> > In common lisp for example the gethash function for the hashtable  
> > returns 2 values, the value in the hash associated at the key and a  
> > boolean value that is true if the value returned is in the hashtable or  
> > false if is the default value. In most of cases this boolean is ignored  
> > and in the code you assign only the first returned value at a variable:  
> > CL-USER\> (setf myvar (gethash key my-hashtable))
> > 
> > What do you think about? (Perhaps I don't know enough Ruby and there is  
> > a way to do the same thing in Ruby 😉 )
> > 
> > Thinking at Ruby I'd like that if I call mymethod defined above with  
> > only a variable it gets the first value:
> > 
> > irb(main):006:0\> a = mymethod  
> > 1  
> > irb(main):007:0\> a  
> > 1  
> > irb(main):008:0\>
> > 
> > and if use a variable with \* then it gets an array with all the values:
> > 
> > irb(main):006:0\> \*a = mymethod  
> > [1, 2]  
> > irb(main):007:0\> a  
> > [1, 2]  
> > irb(main):008:0\>
> > 
> > What do you think about?
> > 
> > Thanks in advance.
> > 
> > Giannandrea
> 
> --  
> [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [20 May 2005 14:38 UTC](https://rubytalk.org/t/multiple-return-and-parallel-assignement/18534/5 "2005-05-20T14:38:02Z")

</div>

James Edward Gray II wrote:

> **···**
>
> > On May 20, 2005, at 8:05 AM, jean wrote:
> > 
> > > What do you think about? (Perhaps I don't know enough Ruby and there is  
> > > a way to do the same thing in Ruby 😉 )
> > > 
> > > Thinking at Ruby I'd like that if I call mymethod defined above with  
> > > only a variable it gets the first value:
> > 
> > Probably not what you want, but:
> > 
> > irb(main):001:0\> def mymethod  
> > irb(main):002:1\> return 1, 2  
> > irb(main):003:1\> end  
> > =\> nil  
> > irb(main):004:0\> one = mymethod.first  
> > =\> 1
> > 
> > Hope that helps.
> 
> I think this helps point out that mymethod is not returning multiple values, but a single value (an Array object) that contains multiple values.
> 
> James  
> --
> 
> > **[Ruby-Doc.org: Documenting the Ruby Language](https://ruby-doc.org/)**
> >
> > Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.
> 
> > **[2007](http://www.rubyxml.com)**
> >
> > Blogtari! powered web site.
> 
> [http://catapult.rubyforge.org](http://catapult.rubyforge.org)  
> [http://orbjson.rubyforge.org](http://orbjson.rubyforge.org)  
> [http://ooo4r.rubyforge.org](http://ooo4r.rubyforge.org)  
> [http://www.jamesbritt.com](http://www.jamesbritt.com)
