# Invoke block in current binding?

**URL:** <https://www.ruby-forum.com/t/invoke-block-in-current-binding/144228>\
**Category:** Ruby\
**Created:** [July 27, 2008, 1:49am UTC](https://www.ruby-forum.com/t/invoke-block-in-current-binding/144228 "2008-07-27T01:49:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kipcole9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/958977/32.png) [@kipcole9](https://www.ruby-forum.com/u/kipcole9)\
**Post date:** [July 27, 2008, 1:49am UTC](https://www.ruby-forum.com/t/invoke-block-in-current-binding/144228/1 "2008-07-27T01:49:16Z")

</div>

I have a design pattern (Rails app, using RestfulAuthentication plugin  
with modifications) that wants to have a method that will temporarily  
elevate user privileges like so:

def sudo  
saved\_current\_user = current\_user  
current\_user = admin\_user  
yield  
current\_user = saved\_current\_user  
end

And in a method, invoke it with

sudo do  
puts “Something done as admin user”  
end

However I know this doesn’t work because at the time the block is  
passed, the binding is fixed to the defining context.

So the question is: what is the right way to achieve the desired result.  
Where the desired result is to pass a block to a method to be executed  
in a different context, without the caller having to know any of the  
implementation details.

Thanks for any help,

–Kip

---

<div class="post-metadata">

**Author:** ![kipcole9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/958977/32.png) [@kipcole9](https://www.ruby-forum.com/u/kipcole9)\
**Post date:** [July 27, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/invoke-block-in-current-binding/144228/2 "2008-07-27T21:20:14Z")

</div>

Kip Cole wrote:

> So the question is: what is the right way to achieve the desired result.  
> Where the desired result is to pass a block to a method to be executed  
> in a different context, without the caller having to know any of the  
> implementation details.

What about this?

Context = Struct.new :user

def sudo  
context = Context.new  
context.user = “admin\_user”  
yield context  
end

outer\_context = Context.new  
outer\_context.user = “normal\_user”  
puts “I am #{outer\_context.user}”

sudo do |context|  
puts “I am #{context.user}”  
end

---

<div class="post-metadata">

**Author:** ![kipcole9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/958977/32.png) [@kipcole9](https://www.ruby-forum.com/u/kipcole9)\
**Post date:** [July 28, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/invoke-block-in-current-binding/144228/3 "2008-07-28T22:59:13Z")

</div>

Joel, thanks very much, much appreciated.

–Kip

Joel VanderWerf wrote:

> Kip Cole wrote:
> 
> > So the question is: what is the right way to achieve the desired result.  
> > Where the desired result is to pass a block to a method to be executed  
> > in a different context, without the caller having to know any of the  
> > implementation details.
> 
> What about this?
> 
> Context = Struct.new :user
> 
> def sudo  
> context = Context.new  
> context.user = “admin\_user”  
> yield context  
> end
> 
> outer\_context = Context.new  
> outer\_context.user = “normal\_user”  
> puts “I am #{outer\_context.user}”
> 
> sudo do |context|  
> puts “I am #{context.user}”  
> end
