[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: value by reference

Florian Gross

1/17/2005 11:12:00 PM

Mohammad Khan wrote:

> def do_something(a, b, c)
> a = a + 1
> b = b + 2
> c = c + 3
> return [a , b, c]
> end
>
> a, b, c = do_something(a, b, c)
>
> I don't like to handle with too many return variables but I had to do it
> in my real project.

Maybe you could return a Hash then?

>>1) Supply lambdas:
>>
>>do_something(
>> lambda { |x| a += x },
>> lambda { |x| b += x },
>> lambda { |x| c += x }
>>)
>
> What is lambdas? sorry.. if I am asking anything very strange !!
> I will still prefer to return multiple variables in stead of using these
> much code!

lambdas are objectified blocks. You can pass them around, assign them to
variables, call methods and more of the stuff you can do with Objects.

The above might look like much code, but there's millions way of making
it shorter. Here's one:

do_something { |ai, bi, ci| a += ai; b += bi; c += ci }

Invokation in the method would change to yield(1, 1, 1).

>>If you find yourself really needing to do this, please follow up with
>>your reasons. I've not yet found many cases where this is necessary and
>>would be interested in getting to know about new ones.

I'm still interested in your reasons for doing this. :)