From: Eric Mahurin Date: 2005-05-21T00:31:51+09:00 Subject: Re: Multiple return and parallel assignement --- James Britt wrote: > 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. yep. return 1,2 and return [1,2] work the same. Also when the RHS or LHS of an assign is only single item and the other side is multiple items you can think of that single item being splatted (*): a = 1,2 # *a = 1,2 # a = [1,2] a,b = [1,2] # a,b = *[1,2] # a,b = 1,2 I've never quite understood why most languages inherently support returning only a single value (Ruby uses an array to make it look like multiple return values). Multiple return values could have been passed back to the caller on the stack just like arguments were passed to it. Maybe Ruby also uses an array to implement multiple arguments. For Ruby, it really doesn't matter too much since we have some niceties (splatting and multiple assign) that give you the similar functionality as true multiple return values. You can't differentiate between something returning a single array and something returning multiple values, but I don't think that is a big deal. Yahoo! Mail Stay connected, organized, and protected. Take the tour: http://tour.mail.yahoo.com/mailtour.html