Argument-per-argument Passing

T

Trans

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.
 
R

Robert Klemme

Trans said:
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
 
T

Trans

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.
 
R

Robert Klemme

Trans said:
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
[1, 2, 3]
=> nil
[1, 2, 3]
=> nil

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top