2

What does the WEBrick instance method mount_proc do (in plain English)?

The docs say:

mount_proc(dir, proc=nil, &block) 
Mounts proc or block on dir and calls it with a WEBrick::HTTPRequest and WEBrick::HTTPResponse

but I'm not clear what mounts proc on dir actually means or does.

Snowcrash
  • 80,579
  • 89
  • 266
  • 376

1 Answers1

3

mount_proc allows you to specify a piece of code (a proc) that will be run when a request comes in. Here's a simple hello world example adapted from the Ruby documentation

require 'webrick'

server = WEBrick::HTTPServer.new :Port => 8000
server.mount_proc '/' do |req, res|
  res.body = 'Hello, world!'
end

trap('INT') { server.stop } # stop server with Ctrl-C
server.start

now point your browser to http://localhost:8000 and you should see

Hello, world!
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • OK, I'm clear what a `proc` is. What's `dir` in this context? And what does it mean by `mount proc on dir`? – Snowcrash Aug 23 '13 at 09:18
  • 1
    `dir` is the request path. “Mount proc on dir” means that when you do `mount_proc '/foo'` the proc will be run when a request to `example.com/foo` is made. – Patrick Oscity Aug 23 '13 at 09:22
  • Did this help you, or do you need additional info? If you've got more questions go ahead :-) – Patrick Oscity Aug 24 '13 at 13:20