# Better error reporting from eval?

**URL:** <https://www.ruby-forum.com/t/better-error-reporting-from-eval/161240>\
**Category:** Ruby\
**Created:** [February 24, 2009, 5:27pm UTC](https://www.ruby-forum.com/t/better-error-reporting-from-eval/161240 "2009-02-24T17:27:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [February 24, 2009, 5:27pm UTC](https://www.ruby-forum.com/t/better-error-reporting-from-eval/161240/1 "2009-02-24T17:27:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [February 24, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/better-error-reporting-from-eval/161240/2 "2009-02-24T17:41:22Z")

</div>

On Feb 24, 2009, at 9:24 AM, matt neuburg wrote:

> 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

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [February 24, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/better-error-reporting-from-eval/161240/3 "2009-02-24T20:06:36Z")

</div>

Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def bar()
> 
> 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.
