# Global variables usage

**URL:** <https://www.ruby-forum.com/t/global-variables-usage/183727>\
**Category:** Ruby\
**Created:** [February 19, 2010, 8:02am UTC](https://www.ruby-forum.com/t/global-variables-usage/183727 "2010-02-19T08:02:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 19, 2010, 8:02am UTC](https://www.ruby-forum.com/t/global-variables-usage/183727/1 "2010-02-19T08:02:50Z")

</div>

I declared two files.One to hold the value of a global variable.And the  
other to print it.

1)to hold variables (var.rb)

Code:

```
$foo = 500

```

1. to print the global variable (gprint.rb)

Code:  
loop do  
puts $foo  
end

But when I print it ,I get the value “nil” being printed not 500.What  
could be the problem?

I did read some tutorials online but dint help.Could some please tell  
why I am not able to access the global variable between two .rb files in  
the same folder?

Please let me know.

---

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 19, 2010, 8:10am UTC](https://www.ruby-forum.com/t/global-variables-usage/183727/2 "2010-02-19T08:10:48Z")

</div>

On Fri, Feb 19, 2010 at 3:02 PM, di vi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. to print the global variable (gprint.rb)
> 
> Code:  
> loop do  
> puts $foo  
> end

where in this code (gprint.rb) does it say that you are accessing the  
file var.rb ?  
read on require and load…

best regards -botp

---

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 19, 2010, 8:26am UTC](https://www.ruby-forum.com/t/global-variables-usage/183727/3 "2010-02-19T08:26:31Z")

</div>

botp wrote:

> On Fri, Feb 19, 2010 at 3:02 PM, di vi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 1. to print the global variable (gprint.rb)
> > 
> > Code:  
> > ï¿½ ï¿½loop do  
> > ï¿½ ï¿½puts $foo  
> > ï¿½ ï¿½end
> 
> where in this code (gprint.rb) does it say that you are accessing the  
> file var.rb ?  
> read on require and load…
> 
> best regards -botp

Load and require execute the complete program…I want only the value of  
global variables to change in the respective .rb file.

---

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 19, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/global-variables-usage/183727/4 "2010-02-19T13:01:15Z")

</div>

2010/2/19 di vi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > where in this code (gprint.rb) does it say that you are accessing the  
> > file var.rb ?  
> > read on require and load…
> > 
> > best regards -botp
> 
> Load and require execute the complete program…I want only the value of  
> global variables to change in the respective .rb file.

Well, there’s an obvious solution to that: place the global variable  
in a file on its own which you load from all places that need it.  
However, I am not sure that this is generally a good idea. What do  
you need that for?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![youthreewire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/90ced4/32.png) [@youthreewire](https://www.ruby-forum.com/u/youthreewire)\
**Post date:** [February 19, 2010, 7:59pm UTC](https://www.ruby-forum.com/t/global-variables-usage/183727/5 "2010-02-19T19:59:50Z")

</div>

On Fri, Feb 19, 2010 at 09:00:47PM +0900, Robert K. wrote:

> > >
> 
> in a file on its own which you load from all places that need it.  
> However, I am not sure that this is generally a good idea. What do  
> you need that for?

. . . or edit the file you’re loading/requiring so that it will only  
conditionally execute code in the file if the file is executed directly,  
and not if it is called as a library using load or require. For  
instance:

```
> cat included_file.rb
#!/usr/bin/env ruby
$foo = 'foo'
$bar = 'bar'
if $0 == __FILE__
  # code to be executed if file
  # called directly for execution
end

> cat including_file.rb
#!/usr/bin/env ruby
load 'included_file.rb'
if $0 = __FILE__
  # do stuff with $foo
  # do stuff with $bar
end

```

Apologies if I’m off-base – I haven’t read the whole thread in detail,  
because I just rejoined the list after a hiatus.
