5

Possible Duplicate:
Undefine variable in Ruby

In php I could do the following:

    $myVariable = 'foo';
    echo $myVariable;
    unset( $myVariable );

How can I do basically the same thing in ruby land?

iconoclast
  • 21,213
  • 15
  • 102
  • 138
Jakcst
  • 605
  • 3
  • 8
  • 16

1 Answers1

-7

You can assign it nil value. Example: your_variable = "yourname" Now, when you want to 'unset' it, you could do your_variable = nil

mkz
  • 212
  • 2
  • 6
  • I assume this will free the memory in the same manner as php? – Jakcst Oct 08 '12 at 19:58
  • 10
    This answer is not correct. Assigning nil to a variable is not the same as undeclaring it. – weexpectedTHIS Oct 08 '12 at 20:02
  • When the garbage collector comes around and sees nothing referencing the given object/variable, it will be released. There's no way to manually force it to release. – joslinm Oct 08 '12 at 20:04
  • What about GC.start ? I looks like I could set the variable to nil then start garbage collection ? – Jakcst Oct 08 '12 at 20:08
  • @Jakcst Setting a variable to nil is still defined in the object space. The garbage collector should mark all variables out of scope for garbage collection, although, I find that making system calls sometimes does not release memory. – scarver2 Mar 08 '14 at 18:34
  • Setting the variable return `nil` and not `NameError: undefined local variable or method`. When variable is deleted it should raise `NameError`. – shantanoo Feb 07 '18 at 05:48