Skip to content

Instantly share code, notes, and snippets.

@tvon
Created January 7, 2014 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvon/8307633 to your computer and use it in GitHub Desktop.
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.
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