CAPSULE

Encapsulated Scripts for Ruby

Capsule is a subclass of Module. A module which is an instance of the Capsule class encapsulates in its scope the top-level methods, top-level constants, and instance variables defined in a Ruby script (and its dependent files) loaded by a Ruby program. This allows use of script files to define objects that can be loaded into a program in much the same way that objects can be loaded from YAML or Marshaled files. There is also an autoimport method which functions like Ruby's autoload but based on Capsule.load.

      # myscript.rb
      VALUE = [1,2,3]
      def run
        puts "#{self} is running."
      end
    
      # program.rb
      require 'capsule'
      myscript = Capsule.load("myscript.rb")
      p myscript::VALUE
      myscript.run
    
      $ ruby program.rb
      [1, 2, 3]
      #<Capsule:myscript.rb> is running.