# Executing code in a variable

**URL:** <https://rubytalk.org/t/executing-code-in-a-variable/45986>\
**Category:** ruby-talk\
**Created:** [20 April 2008 09:43 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986 "2008-04-20T09:43:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zangief\_Ief](https://avatars.discourse-cdn.com/v4/letter/z/8dc957/32.png) [@Zangief\_Ief](https://rubytalk.org/u/Zangief_Ief)\
**Post date:** [20 April 2008 09:43 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/1 "2008-04-20T09:43:13Z")

</div>

Hello,

I have a Ruby code stocked into a Ruby variable like this:

buffer = ' puts "Hello World!" '

Is there a way for execute the current code by using buffer variable ?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [20 April 2008 10:07 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/2 "2008-04-20T10:07:40Z")

</div>

Zangief Ief wrote:

> I have a Ruby code stocked into a Ruby variable like this:
> 
> buffer = ' puts "Hello World!" '
> 
> Is there a way for execute the current code by using buffer variable ?

eval buffer

HTH,  
Sebastian

> **···**
>
> --  
> NP: Depeche Mode - Strangelove  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Christian\_Theil\_Have](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Christian\_Theil\_Have](https://rubytalk.org/u/Christian_Theil_Have)\
**Post date:** [20 April 2008 10:15 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/3 "2008-04-20T10:15:21Z")

</div>

Sure, you can use eval, eg.

irb(main):002:0\> eval 'puts "Hello world"'  
Hello world  
=\> nil

However, eval should be usually avoided. Instead Ruby has the methods  
instance\_eval, class\_eval and module\_eval, which works the same as  
eval but the argumented is executed in the scope of the current object/  
class/module.

Regards,  
Christian

> **···**
>
> On Apr 20, 11:43 am, Zangief Ief \<z4n9...@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I have a Ruby code stocked into a Ruby variable like this:
> > 
> > buffer = ' puts "Hello World!" '
> > 
> > Is there a way for execute the current code by using buffer variable ?
> > 
> > Thanks  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [20 April 2008 10:35 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/4 "2008-04-20T10:35:04Z")

</div>

[http://ruby-doc.org/core/classes/Kernel.html#M005948](http://ruby-doc.org/core/classes/Kernel.html#M005948)

&nbsp;&nbsp;robert

> **···**
>
> On 20.04.2008 11:43, Zangief Ief wrote:
> 
> > I have a Ruby code stocked into a Ruby variable like this:
> > 
> > buffer = ' puts "Hello World!" '
> > 
> > Is there a way for execute the current code by using buffer variable ?

---

<div class="post-metadata">

**Author:** ![Zangief\_Ief](https://avatars.discourse-cdn.com/v4/letter/z/8dc957/32.png) [@Zangief\_Ief](https://rubytalk.org/u/Zangief_Ief)\
**Post date:** [20 April 2008 10:29 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/5 "2008-04-20T10:29:11Z")

</div>

Thanks you a lot! I think I will use instance\_eval to do so.  
🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [20 April 2008 12:50 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/6 "2008-04-20T12:50:20Z")

</div>

Hi --

> > Hello,
> > 
> > I have a Ruby code stocked into a Ruby variable like this:
> > 
> > buffer = ' puts "Hello World!" '
> > 
> > Is there a way for execute the current code by using buffer variable ?
> > 
> > Thanks  
> > --  
> > Posted viahttp://www.ruby-forum.com/.
> 
> Sure, you can use eval, eg.
> 
> irb(main):002:0\> eval 'puts "Hello world"'  
> Hello world  
> =\> nil
> 
> However, eval should be usually avoided. Instead Ruby has the methods  
> instance\_eval, class\_eval and module\_eval, which works the same as  
> eval but the argumented is executed in the scope of the current object/  
> class/module.

The main advantage of instance/class/module\_eval over eval, though, is  
that they can take a block and therefore you don't have to evaluate a  
string. If you do this:

&nbsp;&nbsp;&nbsp;obj.instance\_eval(str)

it's no better or worse, from the point of view of safety, than using  
eval.

David

> **···**
>
> On Sun, 20 Apr 2008, christiantheilhave@gmail.com wrote:
> 
> > On Apr 20, 11:43 am, Zangief Ief \<z4n9...@gmail.com\> wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [20 April 2008 13:30 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/7 "2008-04-20T13:30:04Z")

</div>

It is slightly better because with #instance\_eval you can control what "self" is set to and avoid a certain class of issues:

irb(main):001:0\> class Foo  
irb(main):002:1\> attr\_accessor :bar  
irb(main):003:1\> def work1(s)  
irb(main):004:2\> eval s  
irb(main):005:2\> end  
irb(main):006:1\> def work2(s)  
irb(main):007:2\> Object.new.instance\_eval(s)  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> f=Foo.new  
=\> #\<Foo:0x7ff7acf4\>  
irb(main):011:0\> f.bar="important"  
=\> "important"  
irb(main):012:0\> f.work2 "@bar='messed'"  
=\> "messed"  
irb(main):013:0\> f.bar  
=\> "important"  
irb(main):014:0\> f.work1 "@bar='messed'"  
=\> "messed"  
irb(main):015:0\> f.bar  
=\> "messed"  
irb(main):016:0\>

But this is just a gradual difference - there is still enough damage that can be done by evaluating strings or arbitrary code.

irb(main):016:0\> f.work2 "puts 'ooops!';exit 1"  
ooops!

robert@fussel ~

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 20.04.2008 14:50, David A. Black wrote:
> 
> > Hi --
> > 
> > On Sun, 20 Apr 2008, christiantheilhave@gmail.com wrote:
> > 
> > > On Apr 20, 11:43 am, Zangief Ief \<z4n9...@gmail.com\> wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I have a Ruby code stocked into a Ruby variable like this:
> > > > 
> > > > buffer = ' puts "Hello World!" '
> > > > 
> > > > Is there a way for execute the current code by using buffer variable ?
> > > > 
> > > > Thanks  
> > > > --  
> > > > Posted viahttp://www.ruby-forum.com/.
> > > 
> > > Sure, you can use eval, eg.
> > > 
> > > irb(main):002:0\> eval 'puts "Hello world"'  
> > > Hello world  
> > > =\> nil
> > > 
> > > However, eval should be usually avoided. Instead Ruby has the methods  
> > > instance\_eval, class\_eval and module\_eval, which works the same as  
> > > eval but the argumented is executed in the scope of the current object/  
> > > class/module.
> > 
> > The main advantage of instance/class/module\_eval over eval, though, is  
> > that they can take a block and therefore you don't have to evaluate a  
> > string. If you do this:
> > 
> > &nbsp;&nbsp;&nbsp;obj.instance\_eval(str)
> > 
> > it's no better or worse, from the point of view of safety, than using  
> > eval.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [20 April 2008 13:53 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/8 "2008-04-20T13:53:46Z")

</div>

Hi --

> **···**
>
> On Sun, 20 Apr 2008, Robert Klemme wrote:
> 
> > On 20.04.2008 14:50, David A. Black wrote:
> > 
> > > Hi --
> > > 
> > > On Sun, 20 Apr 2008, christiantheilhave@gmail.com wrote:
> > > 
> > > > On Apr 20, 11:43 am, Zangief Ief \<z4n9...@gmail.com\> wrote:
> > > > 
> > > > > Hello,
> > > > > 
> > > > > I have a Ruby code stocked into a Ruby variable like this:
> > > > > 
> > > > > buffer = ' puts "Hello World!" '
> > > > > 
> > > > > Is there a way for execute the current code by using buffer variable ?
> > > > > 
> > > > > Thanks  
> > > > > --  
> > > > > Posted viahttp://www.ruby-forum.com/.
> > > > 
> > > > Sure, you can use eval, eg.
> > > > 
> > > > irb(main):002:0\> eval 'puts "Hello world"'  
> > > > Hello world  
> > > > =\> nil
> > > > 
> > > > However, eval should be usually avoided. Instead Ruby has the methods  
> > > > instance\_eval, class\_eval and module\_eval, which works the same as  
> > > > eval but the argumented is executed in the scope of the current object/  
> > > > class/module.
> > > 
> > > The main advantage of instance/class/module\_eval over eval, though, is  
> > > that they can take a block and therefore you don't have to evaluate a  
> > > string. If you do this:
> > > 
> > > &nbsp;&nbsp;&nbsp;obj.instance\_eval(str)
> > > 
> > > it's no better or worse, from the point of view of safety, than using  
> > > eval.
> > 
> > It is slightly better because with #instance\_eval you can control what "self" is set to and avoid a certain class of issues:
> > 
> > irb(main):001:0\> class Foo  
> > irb(main):002:1\> attr\_accessor :bar  
> > irb(main):003:1\> def work1(s)  
> > irb(main):004:2\> eval s  
> > irb(main):005:2\> end  
> > irb(main):006:1\> def work2(s)  
> > irb(main):007:2\> Object.new.instance\_eval(s)  
> > irb(main):008:2\> end  
> > irb(main):009:1\> end  
> > =\> nil  
> > irb(main):010:0\> f=Foo.new  
> > =\> #\<Foo:0x7ff7acf4\>  
> > irb(main):011:0\> f.bar="important"  
> > =\> "important"  
> > irb(main):012:0\> f.work2 "@bar='messed'"  
> > =\> "messed"  
> > irb(main):013:0\> f.bar  
> > =\> "important"  
> > irb(main):014:0\> f.work1 "@bar='messed'"  
> > =\> "messed"  
> > irb(main):015:0\> f.bar  
> > =\> "messed"  
> > irb(main):016:0\>
> > 
> > But this is just a gradual difference - there is still enough damage that can be done by evaluating strings or arbitrary code.
> > 
> > irb(main):016:0\> f.work2 "puts 'ooops!';exit 1"  
> > ooops!
> 
> That's the thing -- I think it's more a string thing, and the dangers  
> of untrusted input (which can really do anything), than the question  
> of what self is, since the untrusted input problem can always reassert  
> itself.
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [20 April 2008 14:31 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/9 "2008-04-20T14:31:09Z")

</div>

As David I am not sure that instance\_eval is safer than eval. As the  
following example shows a save eval can be done by deleting all  
dangerous methods before evalling:

module Kernel  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;methods.each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if /^\_\_/ === m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object::send :remove\_method, m  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;instance\_methods.each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;next if /^\_\_/ === m  
&nbsp;&nbsp;&nbsp;&nbsp;Object::send :remove\_method, m  
&nbsp;&nbsp;&nbsp;&nbsp;remove\_method m  
&nbsp;&nbsp;end  
end

eval %\<system "ls -l"\>

Now this might not often be very useful though as we do not have a  
sandbox or to put it better, it is much work to get out  
of the sandbox again as we have to redefine all methods again (well I  
did not safe them in the first place here). Furthermore my sandbox is  
empty!!!

Is there an easy way to do this?

Cheers  
Robert

> **···**
>
> On Sun, Apr 20, 2008 at 3:53 PM, David A. Black \<[dblack@rubypal.com](mailto:dblack@rubypal.com)\> wrote:
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Christopher\_Dicely](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Christopher\_Dicely](https://rubytalk.org/u/Christopher_Dicely)\
**Post date:** [20 April 2008 16:53 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/10 "2008-04-20T16:53:03Z")

</div>

Well, we could use \_why's Freaky Freaky Sandbox:  
[http://code.whytheluckystiff.net/sandbox/](http://code.whytheluckystiff.net/sandbox/)

> **···**
>
> On Sun, Apr 20, 2008 at 7:31 AM, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > On Sun, Apr 20, 2008 at 3:53 PM, David A. Black \<dblack@rubypal.com\> wrote:
> > 
> > As David I am not sure that instance\_eval is safer than eval. As the  
> > following example shows a save eval can be done by deleting all  
> > dangerous methods before evalling:
> > 
> > module Kernel  
> > &nbsp;&nbsp;class \<\< self  
> > &nbsp;&nbsp;&nbsp;&nbsp;methods.each do |m|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if /^\_\_/ === m  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object::send :remove\_method, m  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;instance\_methods.each do |m|  
> > &nbsp;&nbsp;&nbsp;&nbsp;next if /^\_\_/ === m  
> > &nbsp;&nbsp;&nbsp;&nbsp;Object::send :remove\_method, m  
> > &nbsp;&nbsp;&nbsp;&nbsp;remove\_method m  
> > &nbsp;&nbsp;end  
> > end
> > 
> > eval %\<system "ls -l"\>
> > 
> > Now this might not often be very useful though as we do not have a  
> > sandbox or to put it better, it is much work to get out  
> > of the sandbox again as we have to redefine all methods again (well I  
> > did not safe them in the first place here). Furthermore my sandbox is  
> > empty!!!
> > 
> > Is there an easy way to do this?

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [20 April 2008 17:13 UTC](https://rubytalk.org/t/executing-code-in-a-variable/45986/11 "2008-04-20T17:13:35Z")

</div>

Seems to be perfect, now for eval to make sense in the sandbox one has  
to build the castles by onself of course :(.  
R.

> **···**
>
> On Sun, Apr 20, 2008 at 6:53 PM, Christopher Dicely \<cmdicely@gmail.com\> wrote:
> 
> > On Sun, Apr 20, 2008 at 7:31 AM, Robert Dober \<robert.dober@gmail.com\> wrote:  
> > \> On Sun, Apr 20, 2008 at 3:53 PM, David A. Black \<dblack@rubypal.com\> wrote:  
> > \>  
> > \> As David I am not sure that instance\_eval is safer than eval. As the  
> > \> following example shows a save eval can be done by deleting all  
> > \> dangerous methods before evalling:  
> > \>  
> > \> module Kernel  
> > \> class \<\< self  
> > \> methods.each do |m|  
> > \> next if /^\_\_/ === m  
> > \> Object::send :remove\_method, m  
> > \> end  
> > \> end  
> > \> instance\_methods.each do |m|  
> > \> next if /^\_\_/ === m  
> > \> Object::send :remove\_method, m  
> > \> remove\_method m  
> > \> end  
> > \> end  
> > \>  
> > \>  
> > \> eval %\<system "ls -l"\>  
> > \>  
> > \> Now this might not often be very useful though as we do not have a  
> > \> sandbox or to put it better, it is much work to get out  
> > \> of the sandbox again as we have to redefine all methods again (well I  
> > \> did not safe them in the first place here). Furthermore my sandbox is  
> > \> empty!!!  
> > \>  
> > \> Is there an easy way to do this?
> > 
> > Well, we could use \_why's Freaky Freaky Sandbox:  
> > [http://code.whytheluckystiff.net/sandbox/](http://code.whytheluckystiff.net/sandbox/)
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein
