Is there a way that I can load a user-defined Ruby input file into my application in a way that I can access any variables, methods, and classes defined in the input file?. An example input file might look like this:
def my_callback(t)
t ** 2
end
parameter_x = "10 bytes"
parameter_y = my_callback
In my application, I would like to do something like the following:
input = load_input_file
puts input.parameter_x # => "10 bytes"
puts input.parameter_y(2) # => 4
If it isn’t possible to load the input file into an object’s namespace, the next best thing would be local access to the variables (as long as they aren’t globally visible):
load_input_file
puts parameter_x # => "10 bytes"
puts parameter_y(2) # => 4
Is this possible (without manually parsing the input file)?
I think these are your options:
1. Wrap a
modulearoundinput.rb:But your users will need to add:
module Inputandendaround their code. (Not too much to ask for, is it?)2. Stringification
Per your own suggestion:
Strinigfy the
input.rbfile, wrap it with a module, and change the local variables to ivars, then ouput that to ainput_modified.rbfile:In this case, it would be much easier if you told your users to prepend an
@before their variable declarations.