# Use of eval

**URL:** <https://www.ruby-forum.com/t/use-of-eval/94913>\
**Category:** Ruby\
**Created:** [January 10, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/use-of-eval/94913 "2006-01-10T15:36:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [January 10, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/use-of-eval/94913/1 "2006-01-10T15:36:29Z")

</div>

I was thinking of adding a simple command console to a Ruby project  
I’m working on (something like a domain-specific REPL). Under what  
execution environment do the “eval”-ed strings execute, relative to  
the execution environment that is calling the eval method? Can  
classes be opened using eval? Finally, is there anything like Perl’s  
Safe module available in Ruby? Thank you.

-Rich Seagraves

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [January 11, 2006, 5:30am UTC](https://www.ruby-forum.com/t/use-of-eval/94913/2 "2006-01-11T05:30:51Z")

</div>

Rich wrote:

> 

You could try and reuse the already present IRB, plain eval-ing lines  
the user inputs might provide some pitfalls. Just about my US$0.02

David V.

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [January 10, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/use-of-eval/94913/3 "2006-01-10T16:59:21Z")

</div>

Rich wrote:

> I was thinking of adding a simple command console to a Ruby project  
> I’m working on (something like a domain-specific REPL). Under what  
> execution environment do the “eval”-ed strings execute, relative to  
> the execution environment that is calling the eval method?

Normally the binding of the caller is used but you can also provide a  
binding explicitely:

class Test1  
def test1(b)  
eval("@foo = 123", b)  
end  
def test2()  
eval("@bar = 123")  
end  
end  
class Test2  
def test1()  
t = Test1.new  
t.test1(binding)  
[self, t]  
end  
def test2()  
t = Test1.new  
t.test2()  
[self, t]  
end  
end

> > Test2.new.test1  
> > =\> [#\<Test2:0x1019a748 @foo=123\>, #Test1:0x1019a718]
> 
> > Test2.new.test2  
> > =\> [#Test2:0x10198c18, #\<Test1:0x10198be8 @bar=123\>]

> Can  
> classes be opened using eval?

Strings must be syntactic correct Ruby. You can of course add methods  
to  
classes etc.

> Finally, is there anything like Perl’s  
> Safe module available in Ruby? Thank you.

Yes. There is the thread global variable $SAFE which controls what can  
be  
done.

Kind regards

```
robert
```
