# Changing variables mid-script

**URL:** <https://www.ruby-forum.com/t/changing-variables-mid-script/105368>\
**Category:** Ruby\
**Created:** [May 29, 2007, 11:49am UTC](https://www.ruby-forum.com/t/changing-variables-mid-script/105368 "2007-05-29T11:49:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [May 29, 2007, 11:49am UTC](https://www.ruby-forum.com/t/changing-variables-mid-script/105368/1 "2007-05-29T11:49:28Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Say I had a script which read:

task.start  
while $connect do  
task.receive  
end  
task.stop

Could I make the variable $connect false whilst the script it running  
from  
an outside source, other than reading from a file?

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFGW/bb2vy7v+d+psQRAhuHAKD1DCmDiNjGeI/YqZIaKRnHwXYR9ACfYII5  
mX2TMedJJQvqiWXX1TUpdJQ=  
=o3gy  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![no137b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bb73d2/32.png) [@no137b](https://www.ruby-forum.com/u/no137b)\
**Post date:** [May 29, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/changing-variables-mid-script/105368/2 "2007-05-29T20:25:21Z")

</div>

Fred P. wrote:

> Could I make the variable $connect false whilst the script it running from  
> an outside source, other than reading from a file?

Yes. You could use a signal handler or DRb, for example. For signals,  
put this somewhere before your while loop:

trap “USR1” do  
$connect = false  
end

Then "kill -USR1 " will cause $connect to become  
false.

DRb is more complicated, but exposes a much richer interface.
