# Calling a block with parameters in a new scope

**URL:** <https://www.ruby-forum.com/t/calling-a-block-with-parameters-in-a-new-scope/104594>\
**Category:** Ruby\
**Created:** [May 20, 2007, 6:07am UTC](https://www.ruby-forum.com/t/calling-a-block-with-parameters-in-a-new-scope/104594 "2007-05-20T06:07:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubytuesday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@rubytuesday](https://www.ruby-forum.com/u/rubytuesday)\
**Post date:** [May 20, 2007, 6:07am UTC](https://www.ruby-forum.com/t/calling-a-block-with-parameters-in-a-new-scope/104594/1 "2007-05-20T06:07:53Z")

</div>

Calling a block in a new scope is easy enough:

self.instance\_eval &block

And calling a block, passing it parameters is easy enough:

block.call(\*params)

But is there some way to do both? To both change the scope of evaluation  
and pass variables into the block.

---

<div class="post-metadata">

**Author:** ![rubytuesday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@rubytuesday](https://www.ruby-forum.com/u/rubytuesday)\
**Post date:** [May 20, 2007, 1:29pm UTC](https://www.ruby-forum.com/t/calling-a-block-with-parameters-in-a-new-scope/104594/2 "2007-05-20T13:29:25Z")

</div>

Ruby T. wrote:

> Calling a block in a new scope is easy enough:
> 
> self.instance\_eval &block
> 
> And calling a block, passing it parameters is easy enough:
> 
> block.call(\*params)
> 
> But is there some way to do both? To both change the scope of evaluation  
> and pass variables into the block.

Hello,

I’m not sure I fully understand the question being asked here. Can you  
provide a small bit of code to highlight what you are trying to acheive.

Do you wish to pass vaiables into the instance\_eval block?

Any variables in scope when the instance\_eval block is called can be  
used in the instance\_eval block itself. However, if you were to pass a  
proc object into the instance\_eval it would retain the context it was  
created at.

See below for an example of this.

class Test  
def initialize(arg)  
@instance\_variable = arg  
end  
end

test\_obj = Test.new(“initializing\_arg”)

outside = “outside instance\_eval”

proc = Proc.new {|arg| puts self} #self main here

test\_obj.instance\_eval do  
puts @instance\_variable #test\_obj instance variable.  
puts outside #any variable from outside the block can be passed in.  
puts proc #will return main rather than the object  
end

Hmm, I get the feeling I have ranted about stuff that does not help you  
one bit with your question…

Get back to me with a bit more detail and hopefully I’ll be able to  
help!

Cheers,

Matt.

---

<div class="post-metadata">

**Author:** ![rubytuesday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@rubytuesday](https://www.ruby-forum.com/u/rubytuesday)\
**Post date:** [May 20, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/calling-a-block-with-parameters-in-a-new-scope/104594/3 "2007-05-20T13:32:14Z")

</div>

Matt G. wrote:

> Ruby T. wrote:
> 
> > Calling a block in a new scope is easy enough:
> > 
> > self.instance\_eval &block
> > 
> > And calling a block, passing it parameters is easy enough:
> > 
> > block.call(\*params)
> > 
> > But is there some way to do both? To both change the scope of evaluation  
> > and pass variables into the block.
> 
> Hello,
> 
> I’m not sure I fully understand the question being asked here. Can you  
> provide a small bit of code to highlight what you are trying to acheive.
> 
> Do you wish to pass vaiables into the instance\_eval block?
> 
> Any variables in scope when the instance\_eval block is called can be  
> used in the instance\_eval block itself. However, if you were to pass a  
> proc object into the instance\_eval it would retain the context it was  
> created at.
> 
> See below for an example of this.
> 
> class Test  
> def initialize(arg)  
> @instance\_variable = arg  
> end  
> end
> 
> test\_obj = Test.new(“initializing\_arg”)
> 
> outside = “outside instance\_eval”
> 
> proc = Proc.new {|arg| puts self} #self main here
> 
> test\_obj.instance\_eval do  
> puts @instance\_variable #test\_obj instance variable.  
> puts outside #any variable from outside the block can be passed in.  
> puts proc #will return main rather than the object  
> end
> 
> Hmm, I get the feeling I have ranted about stuff that does not help you  
> one bit with your question…
> 
> Get back to me with a bit more detail and hopefully I’ll be able to  
> help!
> 
> Cheers,
> 
> Matt.

Oops, that last ‘puts proc’ should be proc.call(“whatever”)

If full:

class Test  
def initialize(arg)  
@instance\_variable = arg  
end  
end

test\_obj = Test.new(“initializing\_arg”)

outside = “outside instance\_eval”

proc = Proc.new {|arg| puts self} #self main here

test\_obj.instance\_eval do  
puts @instance\_variable #test\_obj instance variable.  
puts outside #any variable from outside the block can be passed in.  
proc.call(“whatever”) #will return main rather than the object  
end

---

<div class="post-metadata">

**Author:** ![rubytuesday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@rubytuesday](https://www.ruby-forum.com/u/rubytuesday)\
**Post date:** [May 20, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/calling-a-block-with-parameters-in-a-new-scope/104594/4 "2007-05-20T17:00:55Z")

</div>

On May 19, 10:07 pm, Ruby T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> self.instance\_eval &block
> 
> And calling a block, passing it parameters is easy enough:
> 
> block.call(\*params)
> 
> But is there some way to do both? To both change the scope of evaluation  
> and pass variables into the block.

[http://eigenclass.org/hiki.rb?instance\_exec](http://eigenclass.org/hiki.rb?instance_exec)

---

<div class="post-metadata">

**Author:** ![rubytuesday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@rubytuesday](https://www.ruby-forum.com/u/rubytuesday)\
**Post date:** [May 20, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/calling-a-block-with-parameters-in-a-new-scope/104594/5 "2007-05-20T18:37:10Z")

</div>

On 5/20/07, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [eigenclass.org](http://eigenclass.org/hiki.rb?instance_exec)

OP We have just discussed this twice recently, maybe you should browse  
the recent posts…

Phrogz: Your link seems outdated,  
c.f.[eigenclass.org](http://eigenclass.org/hiki/bounded+space+instance_exec)  
for a discussion of #undef\_method (bad) vs. #remove\_method (good) and  
please note the potential danger of endless recursion.

Cheers  
Robert
