better error reporting from eval?

M

matt neuburg

This might seem silly, but it is only a reduced version of a real issue.
Consider the following:

class Thing
def ev(what)
eval(what)
end
def bar()
1 + 1
nil + 2 # causing an exception in bar()
end
def foo
bar()
end
end

Thing.new.ev("foo()")

NoMethodError: undefined method `+' for nil:NilClass
method ev
in untitled at line 4
method foo
in untitled at line 11
method ev
in untitled document at line 1
method eval
in untitled at line 15
method ev
in untitled at line 4
at top level
in untitled at line 15

Notice that the error report makes no mention of bar(). This can make
exceptions in an eval difficult to track down. Is there a way to get
better error reporting in an eval? Thx - m.
 
T

Tim Pease

This might seem silly, but it is only a reduced version of a real
issue.
Consider the following:

class Thing
def ev(what)
eval(what)
end
def bar()
1 + 1
nil + 2 # causing an exception in bar()
end
def foo
bar()
end
end

Thing.new.ev("foo()")

def ev(what)
eval(what, binding, __FILE__, __LINE__)
end

You can pass along the filename and the line number that you want
displayed when eval reports errors. The "binding" is simply the
binding of your current Thing instance.

Blessings,
TwP
 
M

matt neuburg

Tim Pease said:
def ev(what)
eval(what, binding, __FILE__, __LINE__)
end

You can pass along the filename and the line number that you want
displayed when eval reports errors. The "binding" is simply the
binding of your current Thing instance.

Amazing - just adding the word "binding" (in the actual issue I'm
having) in the eval call does indeed solve the whole problem. Thx! m.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top