From: Jano Svitok Date: 2007-05-05T06:14:12+09:00 Subject: Re: Playing with Webrick On 5/4/07, El Gato wrote: > I know this is all possible with Rails, but I'm thinking about creating > a little app that uses WEBrick and I'd like to be able to pass instance > variables around from WEBrick to my rhtml. Does anyone know if that's > possible? For example, given this: > > class Foo < HTTPServlet::ERBHandler > def initialize(server, name = "#{Dir::pwd}/htdocs/foo.rhtml") > @foo = "hello" > super > end > end > > ... > server.mount "/foo", Foo > > How could I do > -- foo.rhtml -- > > ... > <%= @foo %> > ... > > > > Thoughts? Am I way off on even thinking this is possible. I know > webrick references the binding in the erbhandler, could I somehow > override it to use my binding instead? This is code from ERB handler: private def evaluate(erb, servlet_request, servlet_response) Module.new.module_eval{ meta_vars = servlet_request.meta_vars query = servlet_request.query erb.result(binding) } end 1. Webrick defines new module and thus new binding for each request, so that request do not interfere with each other. If you'd use one binding it would work different (whether it's good or not depends on your needs) 2. you can set a variable in a binding using eval, i.e. eval("@#{name}=#{value}", binding) or you can set it directly to the created module using instance_variable_set("@#{name}", value) just before the erb.result line 3. you can get a list of instance variable names using instance_variables 4. probably you will want to filter out some of the variables, e.g. @server Not tested, just ideas to explore ;-)