How to use a different Webrick (not the version with current Ruby) outside of using it in Rails

Viewed 261

I'm trying to use Webrick as a simple web server on my local machine. However, rather than use the Webrick included in Ruby 2.6, I'd like to use a copy outside of it. I'm using bundler and here's my Gemfile:

gem 'webrick', path: '/Users/jht/jht-webrick/webrick'

and when I do bundle info webrick I get:

  * webrick (1.6.0)
    Summary: HTTP server toolkit
    Homepage: https://www.ruby-lang.org
    Path: /Users/jht/jht-webrick/webrick 

However, when I start it, it uses the included ruby 2.6 webrick. How can I make it use webrick specified in my Gemfile?

I am using this script (and you can see where I've tried to update the load path and load the source directly):

# $LOAD_PATH.unshift('../webrick/')

# puts "load path: #{$LOAD_PATH}"

# load '../webrick/lib/webrick.rb'

require 'webrick'

root = File.expand_path './public_html'
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root

trap 'INT' do server.shutdown end

server.start
2 Answers

To make bundle resolve dependency I think you need to add this setup:

require "bundler"
Bundler.setup

in top of ruby script file

when using bundler to enforce rubygems versions, then you to run in the context of bundler.

this can be done in 2 ways:

  1. in the executed code by adding

    require "bundler"
    Bundler.setup
    
  2. in the execution command by invoking it using bundler such as bundle exec ruby src/main.rb

Related