Sha256: ad78a8f3e7239d1fefb64283c1dc9b490fb07e4aebfbe77c47e2e19812284671

Contents?: true

Size: 894 Bytes

Versions: 1

Compression:

Stored size: 894 Bytes

Contents

class Object
  def define_if_not_defined(const, value)
    mod = self.is_a?(Module) ? self : self.class
    mod.const_set(const, value) unless mod.const_defined?(const)
    if block_given?
      yield      
      mod.send(:remove_const, const) if mod.const_defined?(const)
    end
  end

  def redefine_without_warning(const, value)
    mod = self.is_a?(Module) ? self : self.class
    if block_given?
      original_value = mod.const_get(const) if mod.const_defined?(const)
      constant_was_set = mod.const_defined?(const)
      mod.send(:remove_const, const) if mod.const_defined?(const)
      mod.const_set(const, value)
      yield
      mod.send(:remove_const, const) if mod.const_defined?(const)
      mod.const_set(const, original_value) if constant_was_set
    else
      mod.send(:remove_const, const) if mod.const_defined?(const)
      mod.const_set(const, value)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
constant-redefinition-1.1.0 lib/constant-redefinition.rb