lib/polyfill/internal_utils.rb in polyfill-1.8.0 vs lib/polyfill/internal_utils.rb in polyfill-1.9.0
- old
+ new
@@ -81,17 +81,32 @@
requested_methods
end
module_function :methods_to_keep
- def create_module
- mod = ::Module.new
+ def create_module(*args)
+ module_name = namify_arguments(*args)
- yield(mod)
-
- Polyfill::Module.const_set("M#{mod.object_id}", mod)
+ if ::Polyfill::Module.const_defined?(module_name, false)
+ ::Polyfill::Module.const_get(module_name, false)
+ else
+ mod = ::Module.new
+ yield(mod)
+ ::Polyfill::Module.const_set(module_name, mod)
+ end
end
module_function :create_module
+
+ def namify_arguments(*args)
+ string = args.map(&:inspect).join
+ # we don't need this to be decodable,
+ # we just need a consistent class-nameable output for a consistent arbitrary input
+ # safest module name is: start with capital then, A-Za-z0-9_
+ encoded = [string].pack('m0')
+ .gsub(%r{[+/=]}, '+' => '_1', '/' => '_2', '=' => '_')
+ "M#{encoded}"
+ end
+ module_function :namify_arguments
def to_str(obj)
begin
unless obj.respond_to?(:to_str)
raise TypeError, "no implicit conversion of #{obj.class} into String"