# Views and bindings

**URL:** <https://www.ruby-forum.com/t/views-and-bindings/154579>\
**Category:** Rails\
**Created:** [December 1, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/views-and-bindings/154579 "2008-12-01T21:02:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-black](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joe-black](https://www.ruby-forum.com/u/joe-black)\
**Post date:** [December 1, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/views-and-bindings/154579/1 "2008-12-01T21:02:03Z")

</div>

hello all, I was hoping someone could describe to me how this works.

Say I have some block of code like this

\<% my\_cool\_block do %\>  
\<% my\_var = 5 %\>  
\<%= some\_helper\_method “My var” %\>  
\<% end %\>

And in my helper

def my\_cool\_block(&block)  
content = capture(&block)  
concat(content, block.binding)  
end

def some\_helper\_method(text)  
"#{text} = #{my\_var}  
end

How can i get some\_helper\_method to ‘know’ about my\_var which is in the  
scope of the my\_cool\_block block? And even better yet, how could I get  
the some\_helper\_method only to work when it is nested inside a  
my\_cool\_block block?

Let me know if I can clarify anything for you.

Thanks!

---

<div class="post-metadata">

**Author:** ![joe-black](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joe-black](https://www.ruby-forum.com/u/joe-black)\
**Post date:** [December 1, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/views-and-bindings/154579/2 "2008-12-01T21:11:04Z")

</div>

On Dec 1, 8:02 pm, Joe B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> scope of the my\_cool\_block block? And even better yet, how could I get  
> the some\_helper\_method only to work when it is nested inside a  
> my\_cool\_block block?

Well you could probably do it by messing around with bindings and eval  
but that sounds like a sucky design. why not do something similar to  
form\_for, ie your my\_cool\_block method yields an object encapsulating  
the state you want to share in its instance variables. usage could be

\<% my\_cool\_block(5) do |helper| %\>  
\<%= helper.some\_helper\_method "My var %\>  
\<% end %\>

you could do other stuff too, eg call helper.with\_value(5) and for the  
block passed to with\_value, my\_var would have value 5

Fred

---

<div class="post-metadata">

**Author:** ![joe-black](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joe-black](https://www.ruby-forum.com/u/joe-black)\
**Post date:** [December 1, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/views-and-bindings/154579/3 "2008-12-01T22:58:00Z")

</div>

> def my\_cool\_block(&block)  
> content = capture(&block)  
> concat(content, block.binding)  
> end

FYI, block.binding is no longer required in Rails 2.2.2, and should  
raise a warning in the log.

---

<div class="post-metadata">

**Author:** ![joe-black](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joe-black](https://www.ruby-forum.com/u/joe-black)\
**Post date:** [December 2, 2008, 12:23am UTC](https://www.ruby-forum.com/t/views-and-bindings/154579/4 "2008-12-02T00:23:56Z")

</div>

Frederick C. wrote:

> On Dec 1, 8:02ï¿½pm, Joe B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > scope of the my\_cool\_block block? And even better yet, how could I get  
> > the some\_helper\_method only to work when it is nested inside a  
> > my\_cool\_block block?
> 
> Well you could probably do it by messing around with bindings and eval  
> but that sounds like a sucky design. why not do something similar to  
> form\_for, ie your my\_cool\_block method yields an object encapsulating  
> the state you want to share in its instance variables. usage could be
> 
> \<% my\_cool\_block(5) do |helper| %\>  
> \<%= helper.some\_helper\_method "My var %\>  
> \<% end %\>
> 
> you could do other stuff too, eg call helper.with\_value(5) and for the  
> block passed to with\_value, my\_var would have value 5
> 
> Fred

Sorry for my ignorance but could you explain how would you do that. How  
would you yield and concat? A quick example would be worth a 1000 words  
🙂
