Dynamic function calls

Discussion in 'Ruby' started by Tomas Fischer, Feb 14, 2007.

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

    --
    Posted via http://www.ruby-forum.com/.
    Tomas Fischer, Feb 14, 2007
    #1
    1. Advertising

  2. --nextPart1672480.qH981OrMCd
    Content-Type: text/plain;
    charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Hi,

    On Wednesday 14 February 2007 14:46, Tomas Fischer wrote:
    > 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


    why don't you pass a parameter to a single "check" method?

    def check(program)
    @directories ||=3D ENV['PATH'].split(File::pATH_SEPARATOR)
    @directories.find { |dir| File.executable?(File.join(dir, program)) }
    end

    def install(program, package =3D nil)
    unless check(program)
    package ||=3D 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=
    =20
    package name to the "install" method.

    HTH
    =2D-=20
    pub 1024D/8D2787EF 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF

    --nextPart1672480.qH981OrMCd
    Content-Type: application/pgp-signature

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.3 (GNU/Linux)

    iD8DBQBF0xnsmVbrPI0nh+8RAvkRAKCFadmFXM9/gljXFACjbDHzAinjZwCgwrHi
    A3tkuYuZRJ6ZWY5iMjOHrS0=
    =G872
    -----END PGP SIGNATURE-----

    --nextPart1672480.qH981OrMCd--
    Marcello Barnaba, Feb 14, 2007
    #2
    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. Honne Gowda A
    Replies:
    2
    Views:
    872
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    756
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Replies:
    2
    Views:
    916
    Bengt Richter
    Aug 1, 2005
  4. Richard Tobin
    Replies:
    24
    Views:
    785
  5. Bob
    Replies:
    5
    Views:
    259
Loading...

Share This Page