Class: WrapInModule::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/wrap_in_module.rb

Overview

A module which is an instance of the Script class encapsulates in its scope the top-level methods, top-level constants, and instance variables defined in a ruby script file (and its subfiles) 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 Marshal files.

See intro.txt[files/intro_txt.html] for an overview.

Defined Under Namespace

Modules: LoadInModuleMethods, ScriptModuleMethods

Instance Method Summary collapse

Constructor Details

#initialize(_module, main_file) {|_self| ... } ⇒ Script

Creates new Script, and loads main_file in the scope of the Script. If a block is given, the script is passed to it before loading from the file, and constants can be defined as inputs to the script.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wrap_in_module.rb', line 21

def initialize(_module, main_file)   # :yields: self
  @__module = _module
  @__module.extend LoadInModuleMethods
  @__module.extend ScriptModuleMethods
  
  yield self if block_given?

  @__module.module_eval do
    # The file with which the Script was instantiated.
    attr_reader :__main_file

    # The directory in which main_file is located, and relative to which
    # #load searches for files before falling back to Kernel#load.
    attr_reader :__dir
    
    # A hash that maps <tt>filename=>true</tt> for each file that has been
    # required locally by the script. This has the same semantics as <tt>$"</tt>,
    # alias <tt>$LOADED_FEATURES</tt>, except that it is local to this script.
    attr_reader :__loaded_features

    @__main_file = File.expand_path(main_file)
    @__dir = File.dirname(@__main_file)
    @__loaded_features = {}
    load_in_module(main_file)
  end
end