Created
February 5, 2016 04:57
-
-
Save dougo-chris/1e515f8175af257f1fcc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see http://stackoverflow.com/questions/1698335/can-i-use-rspec-mocks-to-stub-out-version-constants | |
class Object | |
def self.with_constants(constants, &block) | |
saved_constants = {} | |
constants.each do |constant, val| | |
saved_constants[ constant ] = const_get( constant ) | |
Kernel::silence_warnings { const_set( constant, val ) } | |
end | |
begin | |
block.call | |
ensure | |
constants.each do |constant, val| | |
Kernel::silence_warnings { const_set( constant, saved_constants[ constant ] ) } | |
end | |
end | |
end | |
end | |
class Module | |
def with_constants(constants, &block) | |
saved_constants = {} | |
constants.each do |constant, val| | |
saved_constants[ constant ] = const_get( constant ) | |
Kernel::silence_warnings { const_set( constant, val ) } | |
end | |
begin | |
block.call | |
ensure | |
constants.each do |constant, val| | |
Kernel::silence_warnings { const_set( constant, saved_constants[ constant ] ) } | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment