Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8ed904e786
Fetching contributors…

Cannot retrieve contributors at this time

123 lines (92 sloc) 2.58 kb


Allows you to define constants if not defined on an object (or module) and redefine constants without warning.


gem install constant-redefinition

or in your Gemfile

gem 'constant-redefinition'


Define a constant if not defined:

define_if_not_defined(:A, 1)
 => nil 
 => 1 

Define a constant and redefine it:

define_if_not_defined(:B, 1)
 => nil 
redefine_without_warning(:B, 2)
 => 2 
 => 2 

Redefine a constant which should set the constant:

redefine_without_warning(:C, 3)
 => 3 
 => 3 

Define a constant within a module:

Math.define_if_not_defined(:FOO, 2 * Math::PI)
 => nil 
 => 6.283185307179586 

Define and redefine a constant within a module:

Math.define_if_not_defined(:BAR, 3)
 => nil 
Math.redefine_without_warning(:BAR, 5)
 => 5 
 => 5 

Redefine a constant within a module which should set the constant:

Math.redefine_without_warning(:AMAZING, 3)
 => 3 
 => 3 

You can also define a constant using a block which will unset the constant after the block:

class Freaks
  GOOBLE = 'gobble'
 => "gobble" 

Freaks.define_if_not_defined(:HELLO, 'world') do
    p Freaks::HELLO
 => "world" 

p Freaks::HELLO
NameError: uninitialized constant Freaks::HELLO

You can also redefine a constant using a block, which will reset the constant to its original value after the block:

class Freaks
  GOOBLE = 'gobble'
 => "gobble" 

Freaks.redefine_without_warning(:GOOBLE, 'one of us') do
  p Freaks::GOOBLE

"one of us"
 => "gobble" 

All credit is due to this post:

Contributing to constant-redefinition

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright (c) 2010-2012 David Czarnecki. See LICENSE.txt for further details.

Jump to Line
Something went wrong with that request. Please try again.