Module: WrapInModule::Script::ScriptModuleMethods

Defined in:
lib/wrap_in_module.rb

Overview

def to_s # :nodoc:

"#<#{self.class}:#{File.join(__dir, File.basename(__main_file))}>"

end

Defined Under Namespace

Classes: MissingFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__script_scopeObject (readonly)

Returns the value of attribute __script_scope.



78
79
80
# File 'lib/wrap_in_module.rb', line 78

def __script_scope
  @__script_scope
end

Instance Method Details

#__local_variable_get(name) ⇒ Object

Gets value of local var in the script. Does not see local vars in files loaded or required by that script.



88
89
90
# File 'lib/wrap_in_module.rb', line 88

def __local_variable_get(name)
  eval(name.to_s, __script_scope)
end

#__local_variablesObject

Gets list of local vars in the script. Does not see local vars in files loaded or required by that script.



82
83
84
# File 'lib/wrap_in_module.rb', line 82

def __local_variables
  eval("local_variables", __script_scope)
end

#load(file, wrap = false) ⇒ Object

Loads file into this Script. Searches relative to the local dir, that is, the dir of the file given in the original call to Script.load(file), loads the file, if found, into this Script’s scope, and returns true. If the file is not found, falls back to Kernel.load, which searches on $LOAD_PATH, loads the file, if found, into global scope, and returns true. Otherwise, raises LoadError.

The wrap argument is passed to Kernel.load in the fallback case, when the file is not found locally.

Typically called from within the main file to load additional sub files, or from those sub files.



109
110
111
112
113
114
# File 'lib/wrap_in_module.rb', line 109

def load(file, wrap = false)
  load_in_module(File.join(@__dir, file))
  true
rescue MissingFile
  super
end

#method_added(name) ⇒ Object

This is so that def meth... behaves like in Ruby’s top-level context. The implementation simply calls Module#module_function(name).



74
75
76
# File 'lib/wrap_in_module.rb', line 74

def method_added(name) # :nodoc:
  module_function(name)
end

#require(feature) ⇒ Object

Analogous to Kernel#require. First tries the local dir, then falls back to Kernel#require. Will load a given feature only once.

Note that extensions (*.so, *.dll) can be required in the global scope, as usual, but not in the local scope. (This is not much of a limitation in practice–you wouldn’t want to load an extension more than once.) This implementation falls back to Kernel#require when the argument is an extension or is not found locally.



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/wrap_in_module.rb', line 125

def require(feature)
  unless @__loaded_features[feature]
    @__loaded_features[feature] = true
    file = File.join(@__dir, feature)
    file += ".rb" unless /\.rb$/ =~ file
    load_in_module(file)
  end
rescue MissingFile
  @__loaded_features[feature] = false
  super
end