# Playing with Webrick

**URL:** <https://www.ruby-forum.com/t/playing-with-webrick/103383>\
**Category:** Ruby\
**Created:** [May 4, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/playing-with-webrick/103383 "2007-05-04T22:53:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [May 4, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/playing-with-webrick/103383/1 "2007-05-04T22:53:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [May 4, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/playing-with-webrick/103383/2 "2007-05-04T23:14:51Z")

</div>

On 5/4/07, El Gato [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 😉

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [May 4, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/playing-with-webrick/103383/3 "2007-05-04T23:19:52Z")

</div>

On 5/4/07, Jano S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > …  
> > def evaluate(erb, servlet\_request, servlet\_response)  
> > your needs)  
> > Not tested, just ideas to explore 😉  
> > 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.

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [May 5, 2007, 1:27am UTC](https://www.ruby-forum.com/t/playing-with-webrick/103383/4 "2007-05-05T01:27:28Z")

</div>

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.
