# About lambda and its binding

**URL:** <https://www.ruby-forum.com/t/about-lambda-and-its-binding/151118>\
**Category:** Ruby\
**Created:** [October 17, 2008, 2:40am UTC](https://www.ruby-forum.com/t/about-lambda-and-its-binding/151118 "2008-10-17T02:40:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [October 17, 2008, 2:40am UTC](https://www.ruby-forum.com/t/about-lambda-and-its-binding/151118/1 "2008-10-17T02:40:35Z")

</div>

is there a way to specify the binding of a proc created through lambda?

eg:

$ cat test.rb  
class A  
attr\_reader :foo  
def initialize  
@foo = lambda { do\_that\_thing }  
end  
end

class B  
def do\_that\_thing  
puts “yeah, do it!”  
end

def initialize  
a = A.new  
a.foo.call  
end  
end

B.new

$ ruby test.rb  
test.rb:4:in `initialize': undefined local variable or method`do\_that\_thing’ for #\<A:0x127f28 @foo=#Proc:removed\_email\_address@domain.invalid:4\>  
(NameError)  
from test.rb:15:in `call' from test.rb:15:in`initialize’  
from test.rb:19:in `new’  
from test.rb:19

So… is there a way to call the lambda defined in A with the binding  
of B?  
thanks a lot for any hint…

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [October 17, 2008, 3:15am UTC](https://www.ruby-forum.com/t/about-lambda-and-its-binding/151118/2 "2008-10-17T03:15:48Z")

</div>

Rolando A. wrote:

> So… is there a way to call the lambda defined in A with the binding  
> of B?  
> thanks a lot for any hint…

def initialize  
a = A.new  
instance\_eval(&a.foo)  
end

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [October 17, 2008, 8:40am UTC](https://www.ruby-forum.com/t/about-lambda-and-its-binding/151118/3 "2008-10-17T08:40:54Z")

</div>

Rolando A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> end  
> end
> 
> B.new
> 
> So… is there a way to call the lambda defined in A with the binding  
> of B?  
> thanks a lot for any hint…

Can you provide more context regarding what you’re trying to  
accomplish? Or are you simply wanting to learn more about Ruby’s  
semantics?

For example, parameterizing the lambda may do what you want more  
simply:

class A  
attr\_reader :foo  
def initialize  
@foo = lambda {|x| x.do\_that\_thing }  
end  
end

class B  
def do\_that\_thing  
puts “yeah, do it!”  
end

def initialize  
A.new.foo.call(self)  
end  
end

B.new

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [October 17, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/about-lambda-and-its-binding/151118/4 "2008-10-17T12:48:36Z")

</div>

On 16-10-2008, at 22:15, Mike G. wrote:

> Rolando A. wrote:
> 
> > So… is there a way to call the lambda defined in A with the binding  
> > of B?  
> > thanks a lot for any hint…
> 
> def initialize  
> a = A.new  
> instance\_eval(&a.foo)  
> end

duh… I totally forgot about instance\_eval 🙂  
thanks!!
