Sha256: b09061cceb499e89036151e9e302241d93f3878bf0d9be7013c1b09a37ce8b5f

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

module ObjectSpace
  class << self
    ##
    # Returns all the classes in the object space.
    # Optionally, a block can be passed, for example the following code
    # would return the classes that start with the character "A":
    #
    #  ObjectSpace.classes do |klass|
    #    if klass.to_s[0] == "A"
    #      klass
    #    end
    #  end
    #
    def classes(&block)
      rs = Set.new

      ObjectSpace.each_object(Class).each do |klass|
        if block
          if r = block.call(klass)
            # add the returned value if the block returns something
            rs << r
          end
        else
          rs << klass
        end
      end

      rs
    end

    ##
    # Returns a list of existing classes that are not included in "snapshot"
    # This method is useful to get the list of new classes that were loaded
    # after an event like requiring a file.
    # Usage:
    #
    #   snapshot = ObjectSpace.classes
    #   # require a file
    #   ObjectSpace.new_classes(snapshot)
    #
    def new_classes(snapshot)
      self.classes do |klass|
        if !snapshot.include?(klass)
          klass
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
grape-reload-0.1.0 lib/core_ext/object_space.rb
grape-reload-0.0.4 lib/core_ext/object_space.rb
grape-reload-0.0.3 lib/core_ext/object_space.rb
grape-reload-0.0.2 lib/core_ext/object_space.rb