From: Chris Eskow Date: 2006-08-08T12:12:25+09:00 Subject: Re: Extracting instance variables from a block? Ezra Zygmuntowicz wrote: > > [snip] > > class Web > def page(url, &blk) > @url = url > instance_eval &blk > end > end > > web = Web.new > > web.page '/hello/(\w+)' do |name| > @title = 'Hello' > @name = "Ernie" > end > > puts web.inspect > > => # > > -Ezra Thanks for the suggestion (and the compliment), but the problem with that is that for every page you define, the block of code you associate it with will be called. So, if you define several pages like so: web.page('/') { web.render 'home.rhtml' } web.page('/about') { web.render 'about.rhtml' } web.page('/hello/(\w+)') { |name| @name = name; web.render 'hello.rhtml' } It will render all three templates at once! But it did get me thinking... What if I instance_eval it within Web#run? For example: class Web def initialize #... @obj = Object.new def @obj.b; binding; end end def render(template) #... ERB.new(File.read(template)).run(@obj.b) end def run #... @obj.instance_eval(&action) end end That sort of works nice... But you can't pass in the URL parameters (the ones that your URL regexps match). Is there some way that you can call a Proc object (with parameters), but within the context of a specific object's binding? Chris -- Posted via http://www.ruby-forum.com/.