Class: RememberedEval

Inherits:
Object show all
Defined in:
lib/remembered_evals.rb

Overview

doctest: eval saves away files into some path, then eval’s them from there

>> eval "$a = 3"
>> File.directory? '._remembered_evals'
=> true
>> $a
=> 3

Using it causes the backtraces to behave slightly differently [I'd call it better]
>> eval "begin; raise; rescue Exception => e; $exception = e; end"
>> $exception.backtrace[0]
=> "._remembered_evals/beginraiserescueExceptiona48fc331d57ebd559347525d306d6a67:1:in `eval'"

Class Method Summary collapse

Class Method Details

.cache_code(code_string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/remembered_evals.rb', line 31

def self.cache_code code_string
 path = '._remembered_evals'
 Dir.mkdir path unless File.directory? path
 # create something like /code0xdeadbeef for filename
 fullpath = path + '/' + File.sanitize(code_string[0..31]).gsub('_', '') + Digest::MD5.hexdigest(code_string)[0..63] # don't need too long here
 if File.exist? fullpath
   code_string = File.read(fullpath)
 else
   File.write(fullpath, code_string) # write it out [prefer old data there, so people can edit them by hand if they are experimenting with eval'ed code]
 end
 [code_string, fullpath]
end