Multiple return and parallel assignement

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
                   return 1, 2
                 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 :wink: )

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

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:

   a, = mymethod

or

   a = mymethod[0]

David

--
David A. Black
dblack@wobblini.net

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 :wink: )

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

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
                   return 1, 2
                 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 :wink: )

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/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

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 :wink: )

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

http://catapult.rubyforge.org
http://orbjson.rubyforge.org
http://ooo4r.rubyforge.org
http://www.jamesbritt.com