How can I organize my project in the best way?

Discussion in 'Ruby' started by Christoffer Lernö, Apr 22, 2008.

  1. I'm writing a server for a networked game using ruby, but I keep
    running into issues when organizing them.

    Basically I have a structure looking a bit like this:

    lib/
    testclient/
    gamemodel/
    gamecommands/
    server/
    utils/
    test/
    gamemodel/
    gamecommands/
    server/
    utils/


    The problem is the paths for the requires.

    For a test in say test/gamemodel, I need to require something
    like ../../lib/gamemodel/<file>
    But when running the same test from some test_all.rb in /test, the
    correct require is of course ../lib/gamemodel/<file>
    (The natural way would have been to simply write require <file>)

    It is possible to fix this problem by appending to the load path, but
    this means rows of duplicate code in every test file.

    The problem naturally extends to the lib where you also have to decide
    where the code supposedly is run from.

    Any suggestions on neat ways of solving this problem or are there
    perhaps packages available to make this work automatically?


    /Christoffer
     
    Christoffer Lernö, Apr 22, 2008
    #1
    1. Advertising

  2. Christoffer Lernö

    Trans Guest

    On Apr 22, 6:45 am, Christoffer Lern=F6 <>
    wrote:
    > I'm writing a server for a networked game using ruby, but I keep
    > running into issues when organizing them.
    >
    > Basically I have a structure looking a bit like this:
    >
    > lib/
    > testclient/
    > gamemodel/
    > gamecommands/
    > server/
    > utils/
    > test/
    > gamemodel/
    > gamecommands/
    > server/
    > utils/
    >
    > The problem is the paths for the requires.
    >
    > For a test in say test/gamemodel, I need to require something
    > like ../../lib/gamemodel/<file>
    > But when running the same test from some test_all.rb in /test, the
    > correct require is of course ../lib/gamemodel/<file>
    > (The natural way would have been to simply write require <file>)
    >
    > It is possible to fix this problem by appending to the load path, but
    > this means rows of duplicate code in every test file.
    >
    > The problem naturally extends to the lib where you also have to decide
    > where the code supposedly is run from.
    >
    > Any suggestions on neat ways of solving this problem or are there
    > perhaps packages available to make this work automatically?
    >
    > /Christoffer


    Hi Christoffer,

    When testing your lib you should only need to add lib/ to the load
    path once. For instance you might do:

    ruby -Ilib test/sometest.rb

    T.
     
    Trans, Apr 22, 2008
    #2
    1. Advertising

  3. > Hi Christoffer,
    >
    > When testing your lib you should only need to add lib/ to the load
    > path once. For instance you might do:
    >
    > ruby -Ilib test/sometest.rb


    Yes, but if the test is in

    test/client/tc_connect.rb

    then I might need to require

    test/test_common.rb
    lib/client/connect.rb

    Again I need to add these libs differently depending on where I start
    my test.


    /Christoffer
     
    Christoffer Lernö, Apr 22, 2008
    #3
  4. Christoffer Lernö

    Trans Guest

    On Apr 22, 7:55 am, Christoffer Lern=F6 <>
    wrote:
    > > Hi Christoffer,

    >
    > > When testing your lib you should only need to add lib/ to the load
    > > path once. For instance you might do:

    >
    > > ruby -Ilib test/sometest.rb

    >
    > Yes, but if the test is in
    >
    > test/client/tc_connect.rb
    >
    > then I might need to require
    >
    > test/test_common.rb


    You can add test/ to the load path as well if you need too.

    > lib/client/connect.rb


    This should always be:

    require 'client/connect'

    > Again I need to add these libs differently depending on where I start
    > my test.


    Easiest is to always start from your projects root. Using Rake, for
    instance, makes that automatic. But as an example of manually running
    a test from within the test location:

    $ cd test/client
    $ ruby -I../../lib;../../test tc_connect.rb

    T.
     
    Trans, Apr 22, 2008
    #4
  5. On 22 Apr 2008, at 14:46, Trans wrote:

    > On Apr 22, 7:55 am, Christoffer Lern=F6 <>
    > wrote:
    >>> Hi Christoffer,

    >>
    >>> When testing your lib you should only need to add lib/ to the load
    >>> path once. For instance you might do:

    >>
    >>> ruby -Ilib test/sometest.rb

    >>
    >> Yes, but if the test is in
    >>
    >> test/client/tc_connect.rb
    >>
    >> then I might need to require
    >>
    >> test/test_common.rb

    >
    > You can add test/ to the load path as well if you need too.


    So what you're saying is basically to add everything to the load path =20=

    and then run with that?

    /Christoffer=
     
    Christoffer Lernö, Apr 23, 2008
    #5
  6. Christoffer Lernö

    fedzor Guest

    check out the "need" gem! makes these things simple.


    On Apr 22, 2008, at 6:45 AM, Christoffer Lern=F6 wrote:
    > I'm writing a server for a networked game using ruby, but I keep =20
    > running into issues when organizing them.
    >
    > Basically I have a structure looking a bit like this:
    >
    > lib/
    > testclient/
    > gamemodel/
    > gamecommands/
    > server/
    > utils/
    > test/
    > gamemodel/
    > gamecommands/
    > server/
    > utils/
    >
    >
    > The problem is the paths for the requires.
    >
    > For a test in say test/gamemodel, I need to require something =20
    > like ../../lib/gamemodel/<file>
    > But when running the same test from some test_all.rb in /test, the =20
    > correct require is of course ../lib/gamemodel/<file>
    > (The natural way would have been to simply write require <file>)
    >
    > It is possible to fix this problem by appending to the load path, =20
    > but this means rows of duplicate code in every test file.
    >
    > The problem naturally extends to the lib where you also have to =20
    > decide where the code supposedly is run from.
    >
    > Any suggestions on neat ways of solving this problem or are there =20
    > perhaps packages available to make this work automatically?
    >
    >
    > /Christoffer
    >
    >
    >


    ~ Ira Rizdal

    Go hang a salami, I'm a lasagna hog
     
    fedzor, Apr 25, 2008
    #6
    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. Greg Cyrus

    How to organize a Web-Project

    Greg Cyrus, May 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    601
    Yunus Emre ALPÖZEN [MCAD.NET]
    May 1, 2005
  2. wapsiii

    how best to organize project

    wapsiii, Oct 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    570
    wapsiii
    Oct 16, 2005
  3. C-man
    Replies:
    1
    Views:
    453
    Jim Cobban
    Apr 13, 2004
  4. C-man
    Replies:
    0
    Views:
    402
    C-man
    May 24, 2004
  5. Steve555
    Replies:
    11
    Views:
    446
    Juha Nieminen
    Dec 8, 2008
Loading...

Share This Page