Argument-per-argument Passing

Discussion in 'Ruby' started by Trans, Jan 26, 2005.

  1. Trans

    Trans Guest

    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.
     
    Trans, Jan 26, 2005
    #1
    1. Advertising

  2. "Trans" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Robert Klemme, Jan 26, 2005
    #2
    1. Advertising

  3. Trans

    Trans Guest

    > 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.
     
    Trans, Jan 26, 2005
    #3
  4. "Trans" <> schrieb im Newsbeitrag
    news:...
    >> 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
     
    Robert Klemme, Jan 26, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,944
    gerry
    Apr 10, 2006
  2. Razvan
    Replies:
    1
    Views:
    437
    tony vee
    Sep 10, 2004
  3. Replies:
    5
    Views:
    2,632
  4. Replies:
    0
    Views:
    370
  5. chris brat
    Replies:
    1
    Views:
    667
    chris brat
    May 10, 2006
Loading...

Share This Page