I've been working on trying to better understand Ruby and here's something I'm having trouble with:
$SAFE = 1
puts $SAFE # 1
proc {
$SAFE = 2
puts $SAFE # 2
}.call
puts $SAFE # 1
The above code is partially taken from eRB's source rewritten to better highlight the example. Basically within the proc one can set the value of $SAFE
to whatever value one wants and after the proc, the value of SAFE
returns back to what it was before the proc.
If instead of using the word $SAFE
I change it to a different word, such as $DOOR
:
$DOOR = 1
puts $DOOR
proc {
$DOOR = 2
puts $DOOR
}.call
puts $DOOR
then the value of $DOOR
after the proc is 2 and not 1. Why the difference between the two examples?