I have an encrypted file file = File.new("encrypted.file", "r") that I would like to require in Ruby after decryption. I do not want to decrypt, save, require, and delete the file.
What I do now is:
str=""
file.each do |line|
str+=line
end#do
str = doSomeDecryption(str)
I would like to require str in some way. Any ideas? eval(str) is not an option; it really has to be ‘requireable’.
Strictly speaking, require doesn’t load a file, it loads a resource; with the right overload of Kernel#require, you can do almost anything. Some folks have made versions that use open-uri to load code from a remote server. If you want to implement this in pure Ruby, you will need to use
eval. See http_require (https://github.com/astrails/http_require) for its implementation.If you want to implement this as an extension, you may be able to reduce the potential attack surface area, but you’re still going to be implementing either a save-to-disk-then-require or an
eval. (Strictly speaking, although I can’t find the code in the interpreter right at the moment,requireitself essentially does aneval.)