<commit_before># Extend Kernel with a new function reload_watched_requires that
# reloads all functions that are require'd after this file is
# loaded.

module Kernel
  # TODO initialize this with the mtimes of all currently loaded
  # modules, so that we can reload them too
  REQUIRE_LOAD_TIMES = {}

  alias_method :require_without_watch, :require unless
    Kernel::method_defined?(:require_without_watch)

  def require(*args)
    previously_loaded = $".clone
    value = require_without_watch(*args)
    ($" - previously_loaded).each do |path|
      path = $:.first { |dir| File.exists?(File.join(dir, 'singleton.rb')) } unless path =~ /^\//
      puts "Watching #{path}" if false
      # FIXME race condition if the file was modified between
      # require_without_watch and now
      REQUIRE_LOAD_TIMES[path] = File.mtime(path)
    end
    return value
  end

  def reload_watched_requires()
    REQUIRE_LOAD_TIMES.each do |path, mtime|
      new_mtime = File.mtime(path)
      return unless new_mtime
      return if mtime == new_mtime
      puts "reload #{path} (#{new_mtime} <-> #{mtime})" if false
      load path
      REQUIRE_LOAD_TIMES[path] = new_mtime
    end
  end
end<commit_msg>fix bugs in watch_require; return whether file was reloaded<commit_after># Extend Kernel with a new function reload_watched_requires that
# reloads all functions that are require'd after this file is
# loaded.

module Kernel
  # TODO initialize this with the mtimes of all currently loaded
  # modules, so that we can reload them too
  REQUIRE_LOAD_TIMES = {}

  alias_method :require_without_watch, :require unless
    Kernel::method_defined?(:require_without_watch)

  def require(*args)
    previously_loaded = $".clone
    value = require_without_watch(*args)
    ($" - previously_loaded).each do |feature|
      path = feature
      path = $:.
        reject { |dir| dir =~ /^\w+:/ }.
        map { |dir| File.expand_path(File.join(dir, feature)) }.
        select { |p| File.exists?(p) }.
        first unless path =~ /^\//
      next unless path
      puts "Watching #{path}" if false
      # FIXME race condition if the file was modified between
      # require_without_watch and now
      REQUIRE_LOAD_TIMES[path] = File.mtime(path)
    end
    return value
  end

  ## Returns true iff any file was reloaded
  def reload_watched_requires
    REQUIRE_LOAD_TIMES.any? do |path, mtime|
      new_mtime = File.mtime(path)
      next unless new_mtime
      next if mtime == new_mtime
      load path
      REQUIRE_LOAD_TIMES[path] = new_mtime
      true
    end
  end
end
{'fraction_non_alphanumeric': 0.08932038834951456, 'fraction_numerical': 0.0, 'mean_word_length': 3.161550888529887, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '2832989', 'n_tokens_mistral': 825, 'n_tokens_neox': 801, 'n_words': 249}