<commit_before>require "folder/version"

module Folder
  DEFAULT_SEPARATOR = '.'.freeze

  def fold!(separator = DEFAULT_SEPARATOR)
    klass  = self.class
    folded = klass.new

    each do |key, value|
      next unless key.is_a?(String) and key.include?(separator)

      keys = key.split(separator, -1)
      last = keys.pop

      current = keys.reduce(folded) {|memo, item|
        memo[item].is_a?(klass) ?
          memo[item] : (memo[item] = klass.new)
      }

      current[last] = value unless current[last].is_a?(klass)
      delete key
    end

    merge! folded
  end

  def fold(separator = DEFAULT_SEPARATOR)
    dup.fold! separator
  end
end

Hash.__send__(:include, Folder)
<commit_msg>changed dup to clone for Folder#fold<commit_after>require "folder/version"

module Folder
  DEFAULT_SEPARATOR = '.'.freeze

  def fold!(separator = DEFAULT_SEPARATOR)
    klass  = self.class
    folded = klass.new

    each do |key, value|
      next unless key.is_a?(String) and key.include?(separator)

      keys = key.split(separator, -1)
      last = keys.pop

      current = keys.reduce(folded) {|memo, item|
        memo[item].is_a?(klass) ?
          memo[item] : (memo[item] = klass.new)
      }

      current[last] = value unless current[last].is_a?(klass)
      delete key
    end

    merge! folded
  end

  def fold(separator = DEFAULT_SEPARATOR)
    clone.fold! separator
  end
end

Hash.__send__(:include, Folder)

{'fraction_non_alphanumeric': 0.116214335421016, 'fraction_numerical': 0.0013917884481558804, 'mean_word_length': 2.7842105263157895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6907550', 'n_tokens_mistral': 497, 'n_tokens_neox': 496, 'n_words': 113}