# Separation of Variables

**URL:** <https://www.ruby-forum.com/t/separation-of-variables/67001>\
**Category:** Ruby\
**Created:** [August 30, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/separation-of-variables/67001 "2006-08-30T20:51:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 30, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/separation-of-variables/67001/1 "2006-08-30T20:51:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 30, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/separation-of-variables/67001/2 "2006-08-30T20:59:59Z")

</div>

Martin,

Why can’t you use .ini files? Or, better yet, use a RDBMS or BerkleyDB  
to  
hold them?

rr

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 30, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/separation-of-variables/67001/3 "2006-08-30T21:56:05Z")

</div>

On 8/30/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> certain shot, but are unique to only that shot, such as the length.  
> Additionally, I can only seem to get it to work with $variables, and  
> $shotVersion = 4  
> 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)

1. add methods that calculate dependent data (i.e. Shot#project\_file)
2. 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.
