Skip to content
Allows you to define constants if not defined on an object and redefine constants without warning.
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.document Initial commit to constant-redefinition.
.rspec Swap out RSpec for Test::Unit
.rvmrc Ruby 1.9.3-p0 support
.travis.yml Use version 1.1.0 instead
LICENSE.txt Updating copyright for 2014
Rakefile Updating README with block examples


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-2014 David Czarnecki. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.