# Feeding a codeblock/function with an array or something simi

**URL:** <https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739>\
**Category:** Ruby\
**Created:** [September 4, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739 "2007-09-04T15:37:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739/1 "2007-09-04T15:37:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739/2 "2007-09-04T15:58:27Z")

</div>

On 9/4/07, kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739/3 "2007-09-04T16:11:35Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739/4 "2007-09-04T16:17:36Z")

</div>

2007/9/4, kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739/5 "2007-09-04T16:36:44Z")

</div>

thank you this works nice 🙂

* * *

kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/feeding-a-codeblock-function-with-an-array-or-something-simi/113739/6 "2007-09-04T16:23:38Z")

</div>

On 9/4/07, kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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