I'm using Mash.to_module (from Hashie) to put settings on classes. This works fine, for unit testing my config system I would like to be able to reset this class method. After struggling for 5 hours, I finally found a way to remove the class method settings, but no I can't put it back on... Is there life after undef, or another way to remove a class method? The solution of this question doesn't seem to work. I'm using ruby 2.1.5.
Here is some test code:
class Mash < Hash
def to_module(mash_method_name = :settings)
mash = self
Module.new do |m|
m.send :define_method, mash_method_name.to_sym do
mash
end
end
end
end
class B
class << self
def reset
# singleton_methods.include? :settings # true
# methods.include? :settings # true
# remove_method :settings # method `settings' not defined in #<Class:B>
# send :remove_method, :settings # method `settings' not defined in #<Class:B>
# singleton_class.remove_method, :settings # method `settings' not defined in #<Class:B>
# B.singleton_methods.include? :settings # true
# B.methods.include? :settings # true
# B.send :remove_method, :settings # method `settings' not defined in #<Class:B>
# B.singleton_class.send :remove_method, :settings # method `settings' not defined in #<Class:B>
# methods.include?( :settings ) and undef :settings # unexpected keyword undef
undef :settings if methods.include?( :settings ) # works
end
end
end
B.extend Mash.new.to_module
b = B.new
B.settings # {}
# b.settings # undefined method `settings' <- intented behaviour
# B.instance_eval { remove_method :settings } # `remove_method': method `settings' not defined in B
# B.instance_eval { undef :settings } # WORKS !
B.reset
# B.settings # # undefined method `settings' <- GOOD!
B.extend Mash.new.to_module
B.settings # undefined method `settings' => OOPS, is there life after undef?