Created
January 7, 2014 21:57
-
-
Save tvon/8307633 to your computer and use it in GitHub Desktop.
Dump code to be eval'd in a file so as to get a useful traceback if/when it fails.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def eval_in_file(source, options={}) | |
suffix = options[:suffix] || Time.now.strftime("%s%N") | |
path = options[:path] || "/tmp/" | |
result = nil | |
filename = "#{path}eval-#{suffix}.rb" | |
file = File.new(filename, 'w+') | |
file.write(source) | |
file.rewind | |
begin | |
result = binding.eval(file.read, file.path) | |
rescue | |
raise | |
else | |
File.unlink(filename) | |
ensure | |
file.close | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment