[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Argument-per-argument Passing

Trans

1/26/2005 3:13:00 PM

This came up in an earlier thread. On occasion I run into situations
where I'd like to pass arguments from one method to another in an
"argument-per-argument" fashion. Take a look at the following:

| def give
| return [:a,:b,:c]
| end
|
| def give_each
| return *[:a,:b,:c]
| end
|
| def take(*args)
| p *args
| end
|
| take(:a,:b,:c)
| :a
| :b
| :c
|
| take(give)
| [:a, :b, :c]
|
| take(give_each)
| [:a, :b, :c]

I'm interested in the last exmple being like the first. I.e.

| take(give_each)
| :a
| :b
| :c

Which I am calling argument-per-argument passing.
What is the feasibility of this?

Thanks,
T.

3 Answers

Robert Klemme

1/26/2005 3:29:00 PM

0


"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1106752384.371007.164750@f14g2000cwb.googlegroups.com...
> This came up in an earlier thread. On occasion I run into situations
> where I'd like to pass arguments from one method to another in an
> "argument-per-argument" fashion. Take a look at the following:
>
> | def give
> | return [:a,:b,:c]
> | end
> |
> | def give_each
> | return *[:a,:b,:c]
> | end
> |
> | def take(*args)
> | p *args
> | end
> |
> | take(:a,:b,:c)
> | :a
> | :b
> | :c
> |
> | take(give)
> | [:a, :b, :c]
> |
> | take(give_each)
> | [:a, :b, :c]
>
> I'm interested in the last exmple being like the first. I.e.
>
> | take(give_each)
> | :a
> | :b
> | :c
>
> Which I am calling argument-per-argument passing.
> What is the feasibility of this?

How about

take(*give)
take(*give_each)

Note also, that all your give* methods return the same. You can also do

def give2
return :a,:b,:c
end

def give3
[:a,:b,:c]
end


Regards

robert

Trans

1/26/2005 3:41:00 PM

0

> How about
>
> take(*give)
> take(*give_each)
>
> Note also, that all your give* methods return the same.

Right, but that's exactly what I don't want to do. I.e. changing the
notation used in passing the arguments to #take. Rather I want a way to
"transfer" the arguments directly into the receiveing method (#take) as
dictated by the return of the giving method (#give).

T.

Robert Klemme

1/26/2005 5:58:00 PM

0


"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1106754033.158966.115450@c13g2000cwb.googlegroups.com...
>> How about
>>
>> take(*give)
>> take(*give_each)
>>
>> Note also, that all your give* methods return the same.
>
> Right, but that's exactly what I don't want to do. I.e. changing the
> notation used in passing the arguments to #take. Rather I want a way to
> "transfer" the arguments directly into the receiveing method (#take) as
> dictated by the return of the giving method (#give).

As it stands you have only these options:

a) Use * at the call (which you don't)

b) make take more flexible (as some methods in the std lib):

def take(*a)
a = a[0] if a.size == 1 && Enumerable === a[0]
p a
end

>> take 1,2,3
[1, 2, 3]
=> nil
>> take [1,2,3]
[1, 2, 3]
=> nil

Kind regards

robert