Ian said:
I am not sure what the rules for the request should be, but
p "(1..2)".to_range # 0..2 original request
p "1.2 - 7.2 + 3".to_range # 1..0
p '"a".."c"'.to_range # 0..0
all produce things I wouldn't want.
I see Justin Collins got in the eval suggestion before I
finished looking at it. I note that
eval("1.....7")
causes a syntax mistake. Is there a way to handle syntax
problems parallel to execution errors?
Ian
This is a pretty strange way to go about it, but it will work even in
the presence of errors:
require 'thwait'
def make_range(str)
r = nil
thr = Thread.new do
$SAFE = 4
begin
r = eval(str)
rescue
end
end
ThreadsWait.new(thr).all_waits
r.is_a?(Range) ? r : nil
end
p make_range("1.....7") # nil
p make_range("(1..2)") # 1..2
p make_range("1.2 - 7.2 + 3") # nil
p make_range('"a".."c"') # "a".."c"
p make_range("exit!") # nil
p make_range("load 'evilfile.rb'") # nil
For some reason, trying to use Thread.join will prevent the exception
handling from working.
-Justin