what i am now trying to do is put the Task creation code inside a file
and eval that as a string
----thing.task----
Task.new('task_1') P |t| puts t.name }
----task_runner.rb----
require 'task'
def load_task @t = eval('thing.task')
end
@t = nil
load_task @t.run
and this works...however, the difference is that if an exception is
thrown as part of the Task's action (when it is run) eval craps out with
an exception like
(eval):15
and it brings down the entire script, which is a bad thing because i'm
running each task in a thread. this doesn't happen obviously when i'm
not eval'ing.
can someone explain to me what is going on? I'm thinking it's a
scoping/context thing, but i'm not really sure.
what i am now trying to do is put the Task creation code inside a file
and eval that as a string
----thing.task----
Task.new('task_1') P |t| puts t.name }
----task_runner.rb----
require 'task'
def load_task @t = eval('thing.task')
end
@t = nil
load_task @t.run
and this works...however, the difference is that if an exception is
thrown as part of the Task's action (when it is run) eval craps out with
an exception like
(eval):15
and it brings down the entire script, which is a bad thing because i'm
running each task in a thread. this doesn't happen obviously when i'm
not eval'ing.
can someone explain to me what is going on? I'm thinking it's a
scoping/context thing, but i'm not really sure.
You can handle those exceptions by adding 'rescue' clause to your run method.
i.e.
def run
begin @action.call...
rescue Exception => ex
# do whatever you want with ex
end
end
You have to
Note: in this case the begin and end may be omitted:
def run @action.call...
rescue Exception => ex
# do whatever you want with ex
end
To catch syntax errors, you have to add begin/rescue/end around the
eval command.
Have a look at ruby exception hierarchy in zenspider's Ruby QuickRef,
and carefully consider what do you want to catch and what not (to
allow ctrl-break to function etc.)
Finally, you can control exception propagation using Thread.abort_on_exception.
HTH,
Jano
···
On 9/19/07, Chris Hall <christopher.k.hall@gmail.com> wrote:
Here's my situation
and this works...however, the difference is that if an exception is
thrown as part of the Task's action (when it is run) eval craps out with
an exception like
(eval):15
and it brings down the entire script, which is a bad thing because i'm
running each task in a thread. this doesn't happen obviously when i'm
not eval'ing.
what i am now trying to do is put the Task creation code inside a file
and eval that as a string
----thing.task----
Task.new('task_1') P |t| puts t.name }
^
I'd say you get a syntax error here.
----task_runner.rb----
require 'task'
def load_task @t = eval('thing.task')
end
@t = nil
load_task @t.run
and this works...however, the difference is that if an exception is
thrown as part of the Task's action (when it is run) eval craps out with
an exception like
(eval):15
That statement seems illogical, because eval is only used for loading the task but you claim that eval throws during execution of the task. What am I missing here?
and it brings down the entire script, which is a bad thing because i'm
running each task in a thread. this doesn't happen obviously when i'm
not eval'ing.
can someone explain to me what is going on? I'm thinking it's a
scoping/context thing, but i'm not really sure.
Personally I dislike the way you use instance variables here. Why don't you just let load_task return the new Task and assign the result to a local variable?