Separation of Variables

Discussion in 'Ruby' started by eatmyjunk@gmail.com, Aug 30, 2006.

  1. Guest

    I have a few variables for a program I am attempting to write that I
    need to be able to read into my script. However, certain ones take on a
    different scope than others, so I would like to separate them out into
    groups (in different files, hopefully). It is for film production, so
    I'll try to break it down with that in mind. I hope I'm clear enough.

    The groupings are:
    1. Variables that are used every time the script is run, and never (or
    nearly never) change, such as the project name.
    2. Variables that remain the same for every time the script is run on
    certain shot, but are unique to only that shot, such as the length.
    3. Variables that change every time the script runs, such as the render
    version or the date (not taken from the system time, unfortunately).
    These variables should also be able to compute what the correct file
    for group 2 variables are.
    4. Variables that are calculated from the previous 3 groups and are
    unique with each running of the script, such as the filename of the
    file to be rendered, and its output.

    What I have got working so far is to set all my variables in four
    files, however, I have to manually specify them in the main .rb script.
    Additionally, I can only seem to get it to work with $variables, and
    nothing else. For now, I'd like to have the ruby script run with a flag
    to tell it where to start looking for the variables, and be able to
    pass them on and calculate them until they all get back to the main
    script.

    Here's an example:

    (in instance_variables.rb)
    $shotScene = 10
    $shotShot = 1
    $shotVersion = 4
    ....

    (in main.rb)
    require 'environment_variables.rb'
    require 'instance_variables.rb'
    require 'shot_variables.rb'
    require 'calculated_variables.rb'
    ....

    Although I have not yet set it up, I also want to integrate this group
    of variables (1, 2, 3 and 4) into a database (rails?), so that I can
    have a sort of queue (queued, currently running, and logged entries).
    Although I'll try to muddle my way through a bit more before I
    integrate a db back end, I need to have a solution that can look
    forward to that eventuality.

    Cheers, and thanks,
    Martin
     
    , Aug 30, 2006
    #1
    1. Advertising

  2. Jan Svitok Guest

    On 8/30/06, <> wrote:
    > I have a few variables for a program I am attempting to write that I
    > need to be able to read into my script. However, certain ones take on a
    > different scope than others, so I would like to separate them out into
    > groups (in different files, hopefully). It is for film production, so
    > I'll try to break it down with that in mind. I hope I'm clear enough.
    >
    > The groupings are:
    > 1. Variables that are used every time the script is run, and never (or
    > nearly never) change, such as the project name.
    > 2. Variables that remain the same for every time the script is run on
    > certain shot, but are unique to only that shot, such as the length.
    > 3. Variables that change every time the script runs, such as the render
    > version or the date (not taken from the system time, unfortunately).
    > These variables should also be able to compute what the correct file
    > for group 2 variables are.
    > 4. Variables that are calculated from the previous 3 groups and are
    > unique with each running of the script, such as the filename of the
    > file to be rendered, and its output.
    >
    > What I have got working so far is to set all my variables in four
    > files, however, I have to manually specify them in the main .rb script.
    > Additionally, I can only seem to get it to work with $variables, and
    > nothing else. For now, I'd like to have the ruby script run with a flag
    > to tell it where to start looking for the variables, and be able to
    > pass them on and calculate them until they all get back to the main
    > script.
    >
    > Here's an example:
    >
    > (in instance_variables.rb)
    > $shotScene = 10
    > $shotShot = 1
    > $shotVersion = 4
    > ...
    >
    > (in main.rb)
    > require 'environment_variables.rb'
    > require 'instance_variables.rb'
    > require 'shot_variables.rb'
    > require 'calculated_variables.rb'
    > ...
    >
    > Although I have not yet set it up, I also want to integrate this group
    > of variables (1, 2, 3 and 4) into a database (rails?), so that I can
    > have a sort of queue (queued, currently running, and logged entries).
    > Although I'll try to muddle my way through a bit more before I
    > integrate a db back end, I need to have a solution that can look
    > forward to that eventuality.
    >
    > Cheers, and thanks,
    > Martin


    Hi,

    this is what you can do (among other ways):

    (using YAML as storage format)
    1. create classes that hold the variables, possibly using Struct or OpenStruct.
    There would be Project, Shot, Run, etc.

    Shot = Struct.new:)scene, :shot, :version)

    2. add methods that calculate dependent data (i.e. Shot#project_file)
    3. add methods that initialize data from a Hash. It's easier to
    edit/create YAML files with Hash than with objects, but the other way
    is not that harder, so you may try it as well.

    def Shot.load(filename)
    Shot.new(YAML.load_file(filename))
    end
    or just
    def Shot.load(filename)
    YAML.load(filename)
    end

    Alternatively, just go for ActiveRecord, define your structures and relations.

    The first approach will be a bit easier to setup (i.e. no setup ;-)
    the second is easier in the long term, if you want to use the
    database, create a web interface, etc.
     
    Jan Svitok, Aug 30, 2006
    #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. Wayne Liu

    Question on view and code separation

    Wayne Liu, Jul 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    343
    Wayne Liu
    Jul 25, 2003
  2. tshad
    Replies:
    2
    Views:
    346
    tshad
    Dec 2, 2004
  3. Chris Ashley
    Replies:
    1
    Views:
    345
    Chris Ashley
    Aug 19, 2005
  4. Michael

    Code-separation in ASP.NET 2.0

    Michael, Nov 11, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    698
    Juan T. Llibre
    Nov 12, 2005
  5. klaas

    template separation

    klaas, Jun 28, 2003, in forum: C++
    Replies:
    2
    Views:
    2,164
    Samuele Armondi
    Jun 28, 2003
Loading...

Share This Page