# Semi global variables

**URL:** <https://www.ruby-forum.com/t/semi-global-variables/187569>\
**Category:** Ruby\
**Created:** [April 26, 2010, 3:00am UTC](https://www.ruby-forum.com/t/semi-global-variables/187569 "2010-04-26T03:00:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Don\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Don\_F](https://www.ruby-forum.com/u/Don_F)\
**Post date:** [April 26, 2010, 3:00am UTC](https://www.ruby-forum.com/t/semi-global-variables/187569/1 "2010-04-26T03:00:56Z")

</div>

I have an application where multiple objects are created processing  
different sets of data based upon a configuration specified. Each  
object uses several other objects but those objects must use the  
configuration initially set up in the first primary object. Each of  
the primary objects maintain their own configuration.

How can you have variables defined in the primary object that are  
visible to the other objects it creates?

Don F.

---

<div class="post-metadata">

**Author:** ![Don\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Don\_F](https://www.ruby-forum.com/u/Don_F)\
**Post date:** [April 26, 2010, 3:16am UTC](https://www.ruby-forum.com/t/semi-global-variables/187569/2 "2010-04-26T03:16:44Z")

</div>

On Apr 25, 9:00 pm, Don F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have an application where multiple objects are created processing  
> different sets of data based upon a configuration specified. Each  
> object uses several other objects but those objects must use the  
> configuration initially set up in the first primary object. Each of  
> the primary objects maintain their own configuration.
> 
> How can you have variables defined in the primary object that are  
> visible to the other objects it creates?

Well, we would need to know more to be sure. But one possibility is to  
pass a reference to the primary object to all subsequent objects.

---

<div class="post-metadata">

**Author:** ![Don\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Don\_F](https://www.ruby-forum.com/u/Don_F)\
**Post date:** [April 26, 2010, 4:17am UTC](https://www.ruby-forum.com/t/semi-global-variables/187569/3 "2010-04-26T04:17:00Z")

</div>

On Apr 25, 9:16 pm, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, we would need to know more to be sure. But one possibility is to  
> pass a reference to the primary object to all subsequent objects.

That is an option but I hate to pass around that to every object.

class Driver  
def initialize  
live\_data\_handler = Handler.new(‘live’)  
test\_data\_handler = Handler.new(‘test’)  
end

def handle\_command  
do  
command, data = queuemanager.wait  
case command  
when ‘processlive’  
live\_data\_handler.handle(data)  
when ‘processtest’  
live\_data\_handler.handle(data)  
end  
until command == ‘quit’  
end  
end

class Handler  
def initialize(config)  
@config = getconfig(config)  
@otherconfiginfo = other  
end

def handle(data)  
lots\_of\_different\_objects\_created(data)  
end  
end

this is an example of the structure. same handler code but config  
determines what to do and how.

Don F.

---

<div class="post-metadata">

**Author:** ![Don\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Don\_F](https://www.ruby-forum.com/u/Don_F)\
**Post date:** [April 26, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/semi-global-variables/187569/4 "2010-04-26T15:03:14Z")

</div>

2010/4/26 Don F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > visible to the other objects it creates?
> > 
> > Well, we would need to know more to be sure. But one possibility is to  
> > pass a reference to the primary object to all subsequent objects.
> 
> That is an option but I hate to pass around that to every object.

You might hate it initially but you’ll be glad when coming back to the  
code months later for maintenance. Using global variables of any form  
makes code a) harder to read and understand and b) less flexible  
(because you are bound to that exact global variable). Explicit  
passing of data is the better alternative most of the time. You can  
still organize it in a way that it’s not too much typing overhead.

> ```
> when 'processlive'
> live_data_handler.handle(data)
> when 'processtest'
> live_data_handler.handle(data)
> 
> ```

Shouldn’t that read

test\_data\_handler.handle(data)

?

> ```
> end
> 
> ```
> 
> until command == ‘quit’  
> end  
> end
> 
> class Handler  
> def initialize(config)  
> @config = getconfig(config)  
> @otherconfiginfo = other

Where do you get that “other” from?

> end
> 
> def handle(data)  
> lots\_of\_different\_objects\_created(data)  
> end  
> end
> 
> this is an example of the structure. same handler code but config  
> determines what to do and how.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Don\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Don\_F](https://www.ruby-forum.com/u/Don_F)\
**Post date:** [April 26, 2010, 11:36pm UTC](https://www.ruby-forum.com/t/semi-global-variables/187569/5 "2010-04-26T23:36:08Z")

</div>

On Apr 26, 9:02 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > configuration initially set up in the first primary object. Each of  
> > > > You might hate it initially but you’ll be glad when coming back to the  
> > > > end  
> > > > Shouldn’t that read  
> > > > class Handler  
> > > > end  
> > > > remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/  
> > > > Thanks. Looks like I will just create a configuration object and pass  
> > > > it around.

Other is just a place holder instead of showing more of the code.  
There are lots of config things that are initialized.

Don Frenc
