From: Robert Klemme Date: 2007-09-20T06:00:03+09:00 Subject: Re: exceptions and eval'ing a string On 19.09.2007 19:44, Chris Hall wrote: > Here's my situation > > > I have a setup such as: > > ----task.rb---- > class Task > def initialize(name, &action) > @name = name > @action = action > end > > def run > @action.call(self) > end > end > > ----task_runner.rb---- > require 'task' > > t = Task.new('task_1') { |t| puts t.name } > t.run # => thing > > > 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? Kind regards robert