# Re-binding Proc

**URL:** <https://www.ruby-forum.com/t/re-binding-proc/67279>\
**Category:** Ruby\
**Created:** [September 1, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/re-binding-proc/67279 "2006-09-01T20:21:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 1, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/re-binding-proc/67279/1 "2006-09-01T20:21:16Z")

</div>

Is there a way to call a proc while passing in the binding/scope for  
‘self’? I found an un-responded-to question on this (ruby-talk:12794)  
in 2001 which implies that, at least then, there was no way to do it.

What I’m doing:  
class Foo  
@@handlers = {}

def self.handle\_type( type, &proc )  
@@handlers[type] = proc  
end

attr\_reader :name  
def initialize( name )  
@name = name  
end

def do\_thing( type )  
@@handlers[type].call( self )  
end  
end

Foo.handle\_type( :showname ){ |me|  
puts “My name is ‘#{me.name}’”  
}

f = Foo.new( ‘f’ )  
f.do\_thing( :showname )  
#=\> My name is ‘f’

What I’d like to do instead:

Foo.handle\_type( :showname ){  
puts “My name is ‘#{self.name}’”  
}

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 1, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/re-binding-proc/67279/2 "2006-09-01T20:37:18Z")

</div>

Damn, hit send too soon. **continues**

I realize that I can do this by defining a method (as below), but for  
some reason that feels bad to me. I’d like to do it with Proc objects,  
if possible.

# Works by spamming the method namespace.

class Foo  
def self.handle\_type( type, &proc )  
define\_method( “dothing\_#{type}”, &proc )  
end

# …

def do\_thing( type )  
send “dothing\_#{type}”  
end  
end

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 2, 2006, 1:52am UTC](https://www.ruby-forum.com/t/re-binding-proc/67279/3 "2006-09-02T01:52:43Z")

</div>

Phrogz wrote:

> Is there a way to call a proc while passing in the binding/scope for  
> ‘self’? I found an un-responded-to question on this (ruby-talk:12794)  
> in 2001 which implies that, at least then, there was no way to do it.

I may be just feeling light-headed from COBOL, but  
would #instance\_eval not work here?

> What I’m doing:  
> class Foo  
> @@handlers = {}
> 
> def self.handle\_type( type, &proc )  
> @@handlers[type] = proc  
> end
> 
> attr\_reader :name  
> def initialize( name )  
> @name = name  
> end
> 
> def do\_thing( type )  
> @@handlers[type].call( self )

```
  instance_eval &@@handlers[type]

```

> end  
> end
> 
> Foo.handle\_type( :showname ){ |me|  
> puts “My name is ‘#{me.name}’”  
> }

Foo.handle\_type(:showname) {puts “My name is #{self.name}”}

> f = Foo.new( ‘f’ )  
> f.do\_thing( :showname )  
> #=\> My name is ‘f’
> 
> What I’d like to do instead:
> 
> Foo.handle\_type( :showname ){  
> puts “My name is ‘#{self.name}’”  
> }
