From: Robert Klemme Date: 2003-12-19T17:56:55+09:00 Subject: Re: How to return more than one result from a method? "David A. Black" schrieb im Newsbeitrag news:Pine.LNX.4.44.0312181612570.29763-100000@wobblini... > Hi -- > > On Fri, 19 Dec 2003, Tim Hunter wrote: > > > I'm trying to code a method that has two result values. The values are > > very different objects: an Image (a large and complicated object) and a > > plain ol' Ruby Float. I'm not sure what the best way to handle this is. I > > could return them an Array with 2 elements, but that feels too > > lightweight. I could return a Hash with 2 members. I could define a new > > class with two attributes, but that feels too heavyweight. I even thought > > of having the method yield to a block, passing the two results as > > arguments to the block, but that just seems bizarre. > > > > So I'm soliciting advice. How has this been solved before? > > Could you just assign them directly to variables in the calling scope? > Technically you'd be returning an Array but you don't have to deal > with it directly: > > irb(main):001:0> def two; return 1,2; end > => nil > irb(main):002:0> a,b = two > => [1, 2] > irb(main):003:0> a > => 1 > irb(main):004:0> b > => 2 > > That might be the best way if you don't need to deal with the two > values as a pair beyond getting them back from the method. Even if you need to deal with them as pair this is the appropriate method: irb(main):001:0> def two; return 1,2; end => nil irb(main):002:0> all = two => [1, 2] irb(main):003:0> Regards robert