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-schroeder.de/

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