Sha256: c23bcbf988bf7ff8fc2d97c38ac00f0550a9a0decca9593f4601b1df6760caae

Contents?: true

Size: 1.14 KB

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
padrino-support-0.12.3 lib/padrino-support/core_ext/object_space.rb
padrino-support-0.12.2 lib/padrino-support/core_ext/object_space.rb
padrino-support-0.12.1 lib/padrino-support/core_ext/object_space.rb