Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>In Pure Ruby:</h2> <pre><code>class A TEST = "foo" end puts A::TEST # foo class A TEST = "bar" end # (irb): warning: already initialized constant TEST puts A::TEST # bar </code></pre> <p>In Ruby, you can open a class anytime and redeclare whatever is inside it. It raises a warning only for constants, but it <em>does</em> go ahead and make the change though.</p> <p>Let's even re-write that code a bit more terse:</p> <pre><code>class A TEST = "foo" TEST = "bar" end # (irb):3: warning: already initialized constant TEST </code></pre> <p>The warning appears even if you're not really changing the constant, but just setting it to the same value.</p> <pre><code>class A TEST = "foo" TEST = "foo" end # (irb):3: warning: already initialized constant TEST </code></pre> <p>So overall its just a warning that can be safely ignored.</p> <h2>In Rails:</h2> <p>During Development, Rails reloads whatever changed code in your app. Newer versions of Rails are also pretty smart in figuring out which files have actually changed, and then reloads only those files. So assume you have this controller:</p> <pre><code>class TestController &lt; ApplicationController FOO = "bar" def index ... end end </code></pre> <p>If you make any change inside this, this file will get reloaded. When the file is reloaded, <code>FOO = "bar"</code> is parsed again, and you end up getting the same warning.</p> <h2>Solutions:</h2> <ul> <li><p>If you're using Rails 3.0 or 3.1, try the <a href="https://github.com/paneq/active_reload" rel="noreferrer">active_reload</a> gem and see if your warning goes away (it <em>might</em> unload your classes before reloading them, which <em>might</em> cause the warning to go away)</p></li> <li><p>Use the following to define constants:</p> <pre><code>FOO = "bar" unless const_defined?(:FOO) </code></pre> <p>This will define the constant only if it is not already present. So you will avoid the warnings.</p></li> <li><p>Use a helper method to define constants and automate this</p> <pre><code>module ConstDefiner def define_constant(name, value) const_set(name, value) unless const_defined?(name) end end ActionController::Base.send :extend, ConstDefiner ActiveRecord::Base.send :extend, ConstDefiner # Now in all your controllers/models: # instead of FOO = "value" unless const_defined?(:FOO), use: define_constant :FOO, "value" </code></pre></li> <li><p>These warnings happen <strong>only</strong> in Development mode. You won't be needing all the <code>unless const_defined?(:FOO)</code> in production. Its generally not recommended to have development-specific code in production (unless its really really essential)</p></li> <li><p>Remember that when you say <code>FOO = "bar" unless const_defined?(:FOO)</code>, then even if you really make a change to the <code>FOO</code> constant, it won't get reloaded once its defined. You'll have to stop and start rails server in order to reload it. But the probability of modifying constants during development is a little low when compared to actual code, and not something very frequent. As already said, this doesn't affect production code in any way.</p></li> </ul> <p>Edit: Added link to active_reload gem</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload