Class: RVM::Library
- Inherits:
-
Object
- Object
- RVM::Library
- Defined in:
- lib/rvm/library.rb
Overview
The Library class is made to provide library loading capabilites using any rVM supporte langauges and build a frontend between the rVM code and the Ruby code
A Library can hold the content of more then one, file and the loaded files are not required to have the same langauge
Constant Summary collapse
- CLASS_MAP =
This maps rVM classes to usual Ruby classes so you won’t need to take care of it
{ String => RVM::Classes::String, Fixnum => RVM::Classes::Number, Bignum => RVM::Classes::Number, Float => RVM::Classes::Number, FalseClass => RVM::Classes::Boolean, TrueClass => RVM::Classes::Boolean, Array => RVM::Classes::List }
Instance Method Summary collapse
-
#initialize(language = nil, file = nil, safety = RVM::Safety.new) ⇒ Library
constructor
Initializes library, if anguage and file are given, they are loaded Optionally a saftey object can be passed to handle securing the interpreted code.
-
#load_library(language, file) ⇒ Object
Loads a file into the Library, attention it is executed!.
-
#method_missing(m, *args) ⇒ Object
This is the most important part, so to say, the ruby magic in here If a unknown method is called this gets executed to try to resole it form the loaded libraires.
Constructor Details
#initialize(language = nil, file = nil, safety = RVM::Safety.new) ⇒ Library
Initializes library, if anguage and file are given, they are loaded Optionally a saftey object can be passed to handle securing the interpreted code
25 26 27 28 29 |
# File 'lib/rvm/library.rb', line 25 def initialize(language = nil, file = nil, safety = RVM::Safety.new) @env = RVM::Interpreter.env() @safety = safety load_library(language, file) if language and file end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
This is the most important part, so to say, the ruby magic in here If a unknown method is called this gets executed to try to resole it form the loaded libraires
Priorities are:
1) check the functions
2) check if it was a variable
3) check if the call ended with a '=' then set it's as variable
4) call super
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rvm/library.rb', line 45 def method_missing(m, *args) # First we convert the args vmargs = args.map do |arg| if c = CLASS_MAP[arg.class] c.new(arg) else arg end end n = m.to_s if fun = @env.function(n) fun.call(@env, vmargs) elsif v = @env[n] v.val elsif n =~ /=$/ @env[n.gsub(/=$/,'')] = vmargs.first else super(m,args) end end |