# Eval bind

**URL:** <https://www.ruby-forum.com/t/eval-bind/100580>\
**Category:** Ruby\
**Created:** [July 30, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/eval-bind/100580 "2006-07-30T12:47:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [July 30, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/eval-bind/100580/1 "2006-07-30T12:47:32Z")

</div>

How should I call eval from member function that binds to global  
context?  
Something like

$binding = binding  
class InterpreterCallback  
def methods(expr)  
begin  
return eval(expr, $binding).class.instance\_methods(false).sort  
rescue ScriptError, StandardError  
printf “ERR: %s\n”, $! || ‘exception raised’  
return []  
end  
end  
end

but this does not work when I am calling InterpreterCallback::methods  
from C++ (for example when new local variable in the global context is  
created).

Also I do not understand following

This code:

puts eval(“var” ).class  
puts eval(“var”).class.instance\_methods(false).sort  
var = “”;

produces:

NilClass  
&  
^  
inspect  
nil?  
to\_a  
to\_f  
to\_i  
to\_s  
|

and this code

puts eval(“var” ).class  
puts eval(“var”).class.instance\_methods(false).sort

produces

undefined local variable or method `var’ for main:Object (NameError)

which is exactly what I would expect in the first case also.

Thank you

---

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [July 30, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/eval-bind/100580/2 "2006-07-30T12:56:44Z")

</div>

On 7/30/06, Karel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How should I call eval from member function that binds to global context?

There is the TOPLEVEL\_BINDING, although I don’t if it will help you.

J.

---

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [July 30, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/eval-bind/100580/3 "2006-07-30T13:08:55Z")

</div>

On Sun, Jul 30, 2006 at 07:44:06PM +0900, Karel M. wrote:

> This code:
> 
> puts eval(“var” ).class  
> puts eval(“var”).class.instance\_methods(false).sort  
> var = “”;
> 
> produces:
> 
> NilClass  
> […]  
> which is exactly what I would expect in the first case also.  
> When the “var” String is eval()ed, var="" has already been parsed. Ruby  
> already knows that var refers to a local variable, so “var” will be  
> interpreted correspondingly. When this happens, var hasn’t been assigned  
> to,  
> though, so the returned value is nil.

This is what’s happening:

eval(“a”) # =\> nil  
a = 1 # since the parser has seen this, “a” will be a local  
# in code parsed from now on. Its value is nil until it is  
# initialized.

Compare it to the following situation, where the block is parsed before  
you  
assign to a:

instance\_eval { a } # =\>  
a = nil

# ~\> -:1: undefined local variable or method `a’ for main:Object

(NameError)

# ~\> from -:1

---

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [July 30, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/eval-bind/100580/4 "2006-07-30T13:12:09Z")

</div>

> J.

That works great. Thanks
