# Retain scope when rewriting block source with instance\_eval

**URL:** <https://rubytalk.org/t/retain-scope-when-rewriting-block-source-with-instance-eval/68281>\
**Category:** ruby-talk\
**Created:** [4 August 2013 02:35 UTC](https://rubytalk.org/t/retain-scope-when-rewriting-block-source-with-instance-eval/68281 "2013-08-04T02:35:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 August 2013 02:35 UTC](https://rubytalk.org/t/retain-scope-when-rewriting-block-source-with-instance-eval/68281/1 "2013-08-04T02:35:31Z")

</div>

success.rb works by using the block as is. self.instance\_eval &block

> <https://gist.github.com/bootstraponline/6148819>

I'm rewriting the block source code using ruby2ruby and then passing  
that as a string to self.instance\_eval. fail.rb contains the simple  
failure case. The ary var is out of scope.

Is there a way to retain the  
original scope of the block? I want to pass a string to instance\_eval  
and eval it within the scope of the block.

Thanks.

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

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [4 August 2013 12:24 UTC](https://rubytalk.org/t/retain-scope-when-rewriting-block-source-with-instance-eval/68281/2 "2013-08-04T12:24:16Z")

</div>

instance\_eval by definition evaluates within the scope of the  
receiving instance. You don't need that here. You could just use eval  
with a binding:

&nbsp;&nbsp;&nbsp;&nbsp;eval %(ary \<\< 1; puts 'complete'), block.binding

Regards,  
Sean

> **···**
>
> On Sun, Aug 4, 2013 at 3:35 AM, bootstrap o. \<lists@ruby-forum.com\> wrote:
> 
> > success.rb works by using the block as is. self.instance\_eval &block  
> > [Rewrite block source with instance eval · GitHub](https://gist.github.com/bootstraponline/6148819)
> > 
> > I'm rewriting the block source code using ruby2ruby and then passing  
> > that as a string to self.instance\_eval. fail.rb contains the simple  
> > failure case. The ary var is out of scope.
> > 
> > Is there a way to retain the  
> > original scope of the block? I want to pass a string to instance\_eval  
> > and eval it within the scope of the block.
> > 
> > Thanks.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 August 2013 15:38 UTC](https://rubytalk.org/t/retain-scope-when-rewriting-block-source-with-instance-eval/68281/3 "2013-08-04T15:38:59Z")

</div>

That works. Thank you!

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