Multiple return and parallel assignement

J

jean

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

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
 
D

David A. Black

Hi --

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
 
J

James Edward Gray II

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.

James Edward Gray II
 
B

Brian Schröder

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>
=20
In ruby:
irb(main):003:0> def mymethod
return 1, 2
end
irb(main):005:1> nil
irb(main):006:0> a =3D mymethod
[1, 2]
irb(main):007:0> a
[1, 2]
irb(main):008:0>
=20
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))
=20
What do you think about? (Perhaps I don't know enough Ruby and there is
a way to do the same thing in Ruby ;-) )
=20
Thinking at Ruby I'd like that if I call mymethod defined above with
only a variable it gets the first value:
=20
irb(main):006:0> a =3D mymethod
1
irb(main):007:0> a
1
irb(main):008:0>
=20
and if use a variable with * then it gets an array with all the values:
=20
irb(main):006:0> *a =3D mymethod
[1, 2]
irb(main):007:0> a
[1, 2]
irb(main):008:0>
=20
What do you think about?
=20
Thanks in advance.
=20
Giannandrea
=20

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

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

best regards,

Brian


--=20
http://ruby.brian-schroeder.de/

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

James Britt

James said:
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://www.ruby-doc.org
http://www.rubyxml.com
http://catapult.rubyforge.org
http://orbjson.rubyforge.org
http://ooo4r.rubyforge.org
http://www.jamesbritt.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top