Feeding a codeblock/function with an array or something similar?

Discussion in 'Ruby' started by kazaam, Sep 4, 2007.

  1. kazaam

    kazaam Guest

    Hi,
    I have a codeblock like this:

    def foobar(command,customerrormessage)
    if not system(command)
    puts customerrormessage
    exit
    end
    end


    and then many calls of it like:

    foobar('rm something','Error I could'nt remove something')
    foobar('apt-get install thishere','Error while installing the packet')
    foobar('module-assistant a-i mymodule','Error while installing the module')
    [..]

    In pascal I would simply create a multi-dimensional array[x,y] and fill it with the commands and the corresponding error-messages but how to do this in ruby? There are no arrays like the mentioned on or? Or do you have a even better solution for this?

    greets

    --
    kazaam <>
     
    kazaam, Sep 4, 2007
    #1
    1. Advertising

  2. On 9/4/07, kazaam <> wrote:
    > I have a codeblock like this:
    >
    > def foobar(command,customerrormessage)
    > if not system(command)
    > puts customerrormessage
    > exit
    > end
    > end
    >
    >
    > and then many calls of it like:
    >
    > foobar('rm something','Error I could'nt remove something')
    > foobar('apt-get install thishere','Error while installing the packet')
    > foobar('module-assistant a-i mymodule','Error while installing the module')


    > In pascal I would simply create a multi-dimensional array[x,y] and fill it with the
    > commands and the corresponding error-messages but how to do this in ruby?
    > There are no arrays like the mentioned on or? Or do you have a even better
    > solution for this?


    I assume the order in which you execute the commands is important, so
    I would do it like this (not tested):

    command_list = []
    command_list << {:command => 'rm something', :error_msg => 'I couldn't
    remove something'}
    command_list << {:command => 'apt-get install thishere', :error_msg =>
    'Error while installing the packet'}
    command_list << {:command => 'module-assistant a-i mymodule',
    :error_msg => 'Error while installing the module'}

    process_commands(command_list)

    the process_commands can be something like:

    def process_commands(command_list)
    command_list.each do |command|
    if not system(command[:command])
    puts command[:error_msg]
    end
    end
    end

    Don't know if you want to break the execution of the other commands if
    something fails, in which case you can return from the method inside
    the if, but this can get you started as an idea.

    Jesus.
     
    Jesús Gabriel y Galán, Sep 4, 2007
    #2
    1. Advertising

  3. kazaam

    kazaam Guest

    Re: Feeding a codeblock/function with an array or somethingsimilar?

    Hi thanks,
    I would do this method to make my code smaller and better looking but the whole:

    > command_list << {:command => 'rm something', :error_msg => 'I couldn't
    > remove something'}
    > command_list << {:command => 'apt-get install thishere', :error_msg =>
    > 'Error while installing the packet'}
    > command_list << {:command => 'module-assistant a-i mymodule',
    > :error_msg => 'Error while installing the module'}


    would blow the code even more than my momentan solution :)

    --
    kazaam <>
     
    kazaam, Sep 4, 2007
    #3
  4. 2007/9/4, kazaam <>:
    > Hi,
    > I have a codeblock like this:
    >
    > def foobar(command,customerrormessage)
    > if not system(command)
    > puts customerrormessage
    > exit
    > end
    > end
    >
    >
    > and then many calls of it like:
    >
    > foobar('rm something','Error I could'nt remove something')
    > foobar('apt-get install thishere','Error while installing the packet')
    > foobar('module-assistant a-i mymodule','Error while installing the module')
    > [..]
    >
    > In pascal I would simply create a multi-dimensional array[x,y] and fill it with the commands and the corresponding error-messages but how to do this in ruby? There are no arrays like the mentioned on or? Or do you have a even better solution for this?


    You can create multidimensional array just the same:

    [
    ['rm something','Error I could'nt remove something'],
    ['apt-get install thishere','Error while installing the packet'],
    ['module-assistant a-i mymodule','Error while installing the module'],
    ].each do |cmd, error|
    unless system cmd
    $stderr.puts error
    exit 1
    end
    end

    Kind regards

    robert
     
    Robert Klemme, Sep 4, 2007
    #4
  5. On 9/4/07, kazaam <> wrote:
    > Hi thanks,
    > I would do this method to make my code smaller and better looking but the
    > whole:
    >
    > > command_list << {:command => 'rm something', :error_msg => 'I couldn't
    > > remove something'}
    > > command_list << {:command => 'apt-get install thishere', :error_msg =>
    > > 'Error while installing the packet'}
    > > command_list << {:command => 'module-assistant a-i mymodule',
    > > :error_msg => 'Error while installing the module'}

    >
    > would blow the code even more than my momentan solution :)


    It's easy to read this from a YAML configuration file or from a DB, so
    the actual list of commands is not hardcoded. It all depends on the
    complexity of your application. Of course you can also use other
    structures like a multidimensional array, or a unidimensional array
    with [cmd1, msg1, cmd2, msg2...], but I like the hash cause it makes
    clear what each element is. I'd rather not have arrays where you need
    to know that the first element is something and the second is
    something else, but for simple cases it might be enough. YMMV, though.

    Jesus.
     
    Jesús Gabriel y Galán, Sep 4, 2007
    #5
  6. kazaam

    kazaam Guest

    Re: Feeding a codeblock/function with an array or somethingsimilar?

    thank you this works nice :)

    ---
    kazaam <>
     
    kazaam, Sep 4, 2007
    #6
    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. Replies:
    0
    Views:
    347
  2. Matthias Buelow
    Replies:
    4
    Views:
    265
    Matthias Buelow
    Aug 22, 2008
  3. Nime

    inline codeblock doesnt run

    Nime, Jan 28, 2009, in forum: ASP .Net
    Replies:
    12
    Views:
    1,837
    MarkD
    Feb 19, 2009
  4. Jean-Daniel
    Replies:
    2
    Views:
    225
    Jean-Daniel
    Feb 12, 2011
  5. Mayank Jain

    different output in ideone and codeblock

    Mayank Jain, Jan 27, 2013, in forum: C Programming
    Replies:
    2
    Views:
    217
    Barry Schwarz
    Jan 27, 2013
Loading...

Share This Page