Forwarding Forwarding - 3 months ago 10
Ruby Question

How to uninitialize/undefine a variable in Ruby so I don't get warnings in console?

I keep getting this warning in console, it's cluttering my

puts
output, and I want it gone:

d.rb:24: warning: previous definition of VariableA was here
d.rb:86: warning: already initialized constant VariableA


Tried doing this after using them:

VariableA = nil
VariableB = nil


Important note: These variables are re-used in a loop.

Answer

In ruby whenever you use a capital letter first letter you are creating a constant. You will be warned when it is redefined.

For example

class
  THIS_IS_A_CONSTANT = 42
  ThisIsAlsoAConstant = "The answer!"

  @@this_is_a_class_variable

  def a_method
   @this_is_a_member_variable = true
   this_is_a_local_variable = true
  end

end

If you create a constant inside a method, you will get re-definition error the second time you call the method.