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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 1, 8:02 pm, Joe Blow <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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?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> > Let me know if I can clarify anything for you. > > Thanks! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Dec 1, 8:02�pm, Joe Blow <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 > > FredSorry 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 :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---