Dynamic function calls

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

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

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