Passing array instead of individual parameters?

Discussion in 'Ruby' started by Stefan Kruger, Nov 10, 2008.

  1. [New to this fine language, from a Perl background]

    I expected (wrongly) the following to work -

    def myroutine(one, two, three)
    puts "first: #{one} second: #{two} third: #{three}"

    myroutine('brave', 'new', 'world') # as expected

    # The unexpected (to me)

    myparamlist = ['brave', 'new', 'world']

    myroutine(myparamlist) # fails

    So, my question is - is there a way of calling a routine expecting N
    parameters with a list containing N elements instead?

    Specifically, using the dbi module, I expected to be able to say:

    sth.prepare("insert into foo (one, two, three) values (?, ?, ?)")

    but instead I need to loop through the list and bind each one in turn?

    Many thanks,

    Stefan Kruger, Nov 10, 2008
    1. Advertisements

  2. Stefan Kruger

    Matthew Moss Guest

    Matthew Moss, Nov 10, 2008
    1. Advertisements

  3. Alle Monday 10 November 2008, Stefan Kruger ha scritto:

    The *, when put in front of an array in an argument list transforms each
    element of the array in an argument to the method. It is sometimes called the
    "splat operator", even if it's not a true operator.

    Stefano Crocco, Nov 10, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.