Paths, gentleman, paths

Discussion in 'Ruby' started by Ohad Lutzky, Nov 6, 2006.

  1. Ohad Lutzky

    Ohad Lutzky Guest

    I have this bit of code in the beginning of an application I'm writing
    (in main.rb):

    require 'pathname'

    p = Pathname.new($0)

    if p.basename.to_s == 'main.rb'
    Dir.chdir p.parent.to_s
    end

    require 'gui/main_window'
    ... # (rest of requires, application itself)

    The check whether it's "main.rb" or not is so this doesn't happen for
    Rake. Now, it works well, but it's ugly. The reason I don't just go
    ahead and use ':' is that I use glade, as well as some other files which
    I need to be able to find. This is currently in development, so I'd
    prefer to avoid forcing people to install the program in predetermined
    locations (/usr/local/bin, /usr/local/share/my_app_files, et cetera).

    Any cleaner solution?

    --
    Posted via http://www.ruby-forum.com/.
     
    Ohad Lutzky, Nov 6, 2006
    #1
    1. Advertising

  2. On 06.11.2006 14:13, Ohad Lutzky wrote:
    > I have this bit of code in the beginning of an application I'm writing
    > (in main.rb):
    >
    > require 'pathname'
    >
    > p = Pathname.new($0)
    >
    > if p.basename.to_s == 'main.rb'
    > Dir.chdir p.parent.to_s
    > end
    >
    > require 'gui/main_window'
    > ... # (rest of requires, application itself)
    >
    > The check whether it's "main.rb" or not is so this doesn't happen for
    > Rake. Now, it works well, but it's ugly. The reason I don't just go
    > ahead and use ':' is that I use glade, as well as some other files which
    > I need to be able to find. This is currently in development, so I'd
    > prefer to avoid forcing people to install the program in predetermined
    > locations (/usr/local/bin, /usr/local/share/my_app_files, et cetera).
    >
    > Any cleaner solution?
    >


    Dir.chdir( File.dirname( $0 ) ) if File.basename $0 == 'main.rb'

    or

    dir, file = File.split $0
    Dir.chdir dir if file == 'main.rb'

    Cheers

    robert
     
    Robert Klemme, Nov 6, 2006
    #2
    1. Advertising

  3. --------------enig50683BDD55C0391366B6186E
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: quoted-printable

    Ohad Lutzky wrote:
    > The check whether it's "main.rb" or not is so this doesn't happen for=20
    > Rake. Now, it works well, but it's ugly. The reason I don't just go=20
    > ahead and use ':' is that I use glade, as well as some other files whic=

    h=20
    > I need to be able to find. This is currently in development, so I'd=20
    > prefer to avoid forcing people to install the program in predetermined =


    > locations (/usr/local/bin, /usr/local/share/my_app_files, et cetera).
    >=20


    Have your startup script/s determine the "root" of your application's
    directory structure, and use a global to refer to all other resources
    using absolute paths from this root path? (Yes, I know globals are evil,
    but the current working directory is global state too, so it's at least
    not a worse solution.)

    Depending on being chdirred into your directory structure or on
    predetermined install locations is Bad, smells too much of "I know
    what's good for my users better than them" to me.

    David Vallner


    --------------enig50683BDD55C0391366B6186E
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: OpenPGP digital signature
    Content-Disposition: attachment; filename="signature.asc"

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (MingW32)

    iD8DBQFFT+wzy6MhrS8astoRAn5uAJ9eYWZnwuKA4+jgiKC1TFeK+ojxRACeKBSM
    pMcXlL35JjNdq1Edqmf486M=
    =cZjf
    -----END PGP SIGNATURE-----

    --------------enig50683BDD55C0391366B6186E--
     
    David Vallner, Nov 7, 2006
    #3
    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. Subroto Datta

    Re: Multi Cycle path and False paths

    Subroto Datta, Jul 26, 2003, in forum: VHDL
    Replies:
    9
    Views:
    23,513
    Nicholas C. Weaver
    Aug 2, 2003
  2. =?ISO-8859-1?Q?Andr=E9s?=

    Global Reset paths

    =?ISO-8859-1?Q?Andr=E9s?=, Mar 9, 2005, in forum: VHDL
    Replies:
    11
    Views:
    1,876
    A Beaujean
    Mar 15, 2005
  3. Tor Rest

    Copy using UNC paths

    Tor Rest, Aug 11, 2003, in forum: Perl
    Replies:
    1
    Views:
    3,768
    J├╝rgen Exner
    Aug 12, 2003
  4. Jim Rendant

    Paths question

    Jim Rendant, Sep 30, 2003, in forum: Perl
    Replies:
    5
    Views:
    417
    Gunnar Hjalmarsson
    Oct 1, 2003
  5. Noah
    Replies:
    5
    Views:
    846
Loading...

Share This Page