Playing with Webrick

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?

On 5/4/07, El Gato [email protected] wrote:

end

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 :wink:

On 5/4/07, Jano S. [email protected] wrote:

end

def evaluate(erb, servlet_request, servlet_response)
your needs)
Not tested, just ideas to explore :wink:
And I’ve forgotten:

  1. have a look at rails, how they do it… I don’t have them installed
    now, so I can’t tell you where to look, just search for ERB or similar
    (IIRC evaluate_locals is the exact method to look for)

J.

Jano S. wrote:

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)

Yeah, it appears that if I just monkey patch and skip the enclosing
Module.new.module_eval things work as I’d like (full access to variables
and methods). I just feel like I must be doing something really dirty
here, though I’m not smart enough to understand exactly what. Has
anyone done anything like this with webrick before? I really don’t need
the whole Rails framework (or probably even camping for that matter).
Plus, I’m partially trying to avoid getting the admins to install gems,
and I’m partially just trying to understand how this would work.

I’m still looking through Rails stuff to see how they do it… haven’t
found it yet. That’s a pretty big code base.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs