[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Multiple return and parallel assignement

jean

5/20/2005 1:01:00 PM

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

4 Answers

dblack

5/20/2005 1:13:00 PM

0

James Gray

5/20/2005 1:17:00 PM

0

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.

James Edward Gray II


Brian Schröder

5/20/2005 1:23:00 PM

0

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

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


--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...


james_b

5/20/2005 2:38:00 PM

0

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

http://www.ru...
http://www.r...
http://catapult.rub...
http://orbjson.rub...
http://ooo4r.rub...
http://www.jame...