<commit_before># depends on: module.rb

module ObjectSpace

  # HACK: Tryes to handle as much as it can.
  # Any way to get a list of all instances? Or of all objects?
  def self.each_object(what = nil, &block)

    raise TypeError, "class or module required" if what and not what.is_a? Module
    
    if what == nil
      raise ArgumentError, "ObjectSpace cannot loop through all objects yet"
    end

    if what == Class
      return recursive_loop(Object, block) { |a_class| a_class.__subclasses__ }
    end

    if  what == Module
      return recursive_loop(Object, block) do |a_module|
        a_module.constants.inject([]) do |list, const|
          begin
            const = a_module.const_get const
            list << const if const.is_a? Module
            list += const.__subclasses__ if const.is_a? Class
          rescue NameError, LoadError # Handles autoloading.
          end
          list
        end
      end
    end

    if what == Fixnum
      Platform::Fixnum.MIN.upto(Platform::Fixnum.MAX, &block)
      return Platform::Fixnum.MAX - Platform::Fixnum.MIN
    end

    if [TrueClass, FalseClass, NilClass].include? what
      yield what.new
      return 1
    end

    if what == GlobalVariables
      yield Globals
      return 1
    end

    if defined?(Singleton) and what.ancestors.include?(Singleton)
      return 0 unless what.instance_eval { _instantiate? }
      yield what.instance
      return 1
    end

    if what.is_a? MetaClass
      yield what.attached_instance
      return 1
    end

    # Remove the following line when each_object(nil) is implemented.
    raise ArgumentError, "ObjectSpace doesn't support '#{what}' yet"
    count = 0
    each_object do |obj|
      if obj.is_a? what
        count += 1
        yield obj
      end<commit_msg>Some comments.<commit_after># depends on: module.rb

module ObjectSpace

  # HACK: Tryes to handle as much as it can.
  # Any way to get a list of all instances? Or of all objects?
  def self.each_object(what = nil, &block)

    raise TypeError, "class or module required" if what and not what.is_a? Module
    
    if what == nil
      raise ArgumentError, "ObjectSpace cannot loop through all objects yet"
    end

    # Finds all classes by recursivly looping through subclasses.
    if what == Class
      return recursive_loop(Object, block) { |a_class| a_class.__subclasses__ }
    end

    # Finds all modules by looping through the constants of all classes (hence
    # __subclasses__) and checking whether those are modules.
    if  what == Module
      return recursive_loop(Object, block) do |a_module|
        # Get all modules that are helt in a_module.constants plus
        # subclasses of a_module if a_module is a class.
        a_module.constants.inject([]) do |list, const|
          begin
            const = a_module.const_get const
            list << const if const.is_a? Module
            list += const.__subclasses__ if const.is_a? Class
          rescue NameError, LoadError # Handles autoloading.
          end
          list
        end
      end
    end

    # Looping through all fixnums.
    if what == Fixnum
      Platform::Fixnum.MIN.upto(Platform::Fixnum.MAX, &block)
      return Platform::Fixnum.MAX - Platform::Fixnum.MIN
    end

    # Those are singeltons.
    if [TrueClass, FalseClass, NilClass].include? what
      yield what.new
      return 1
    end

    # In the unlikely case that someone would create another instance
    # of GlobalVariables, this wouldn't work.
    if what == GlobalVariables
      yield Globals
      return 1
    end

    # This is a singelton pattern, too.
    if what.is_a? MetaClass
      yield what.attached_instance
      return 1
    end

    # If this is a Singelton, check whether it already has an instance.
    if defined?(Singleton) and what.ancestors.include?(Singleton)
      return 0 unless what.instance_eval { _instantiate? }
      yield what.instance
      return 1
    end

    # Remove the following line when each_object(nil) is implemented.
    raise ArgumentError, "ObjectSpace doesn't support '#{what}' yet"

    # Simply loop through all objects an checkt wheter those are a +what+.
    count = 0
    each_object do |obj|
      if obj.is_a? what
        count += 1
        yield obj
      end
{'fraction_non_alphanumeric': 0.06947368421052631, 'fraction_numerical': 0.0032748538011695906, 'mean_word_length': 2.382911392405063, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5258837', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1219, 'n_words': 456}