# Dynamic function calls

**URL:** <https://www.ruby-forum.com/t/dynamic-function-calls/85543>\
**Category:** Ruby\
**Created:** [February 14, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/dynamic-function-calls/85543 "2007-02-14T14:46:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomas](https://www.ruby-forum.com/u/tomas)\
**Post date:** [February 14, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/dynamic-function-calls/85543/1 "2007-02-14T14:46:11Z")

</div>

Hi,

I want to write an installer, which checks, if sth. is present on the  
target system and if not, the installer will install it. For example,  
the installer checks if zip is installed.

So I have tons of check functions and corresponding install funtions:

def check\_zip  
…  
return true / false  
end

def install\_zip  
…  
system(apt-get install zip)  
end

My idea is to use an array, which contains the result of the checking  
process  
array= [zip, …] means, that we have to install zip. Is it possible to  
“connect” it  
to the corresponding install function?

for each element in array  
call install\_ array[index]  
end

Thanks.

Best regards,

Tomas

---

<div class="post-metadata">

**Author:** ![tomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomas](https://www.ruby-forum.com/u/tomas)\
**Post date:** [February 14, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/dynamic-function-calls/85543/2 "2007-02-14T14:56:30Z")

</div>

On 14.02.2007 14:46, Tomas F. wrote:

> return true / false  
> “connect” it  
> to the corresponding install function?
> 
> for each element in array  
> call install\_ array[index]  
> end

array.each do |name|  
send “install\_#{name}”  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![tomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomas](https://www.ruby-forum.com/u/tomas)\
**Post date:** [February 14, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/dynamic-function-calls/85543/3 "2007-02-14T15:18:33Z")

</div>

Hi,

On Wednesday 14 February 2007 14:46, Tomas F. wrote:

> return true / false  
> end
> 
> def install\_zip  
> …  
> system(apt-get install zip)  
> end

why don’t you pass a parameter to a single “check” method?

def check(program)  
@directories ||= ENV[‘PATH’].split(File::PATH\_SEPARATOR)  
@directories.find { |dir| File.executable?(File.join(dir, program)) }  
end

def install(program, package = nil)  
unless check(program)  
package ||= program  
system “apt-get install #{package}”  
end  
end

%w(zip unzip rar unrar).each { |program| install program }

obivously with some software such as unrar-nonfree you should pass also  
the  
package name to the “install” method.

HTH
