Skip to content

Make syntax highlighting work when the coderay gem is installed but n…

gitlab-qa-bot requested to merge dont-use-kernel-require into master

Created by: myronmarston

…ot on your path.

Apparently, ::Kernel.require is only ruby's built-in require (based on the $LOAD_PATH) and does not get the enhanced capabilities of rubygems when rubygems is loaded. Rubygems only monkey patches ::Kernel#require, not ::Kernel.require.

Originally, I had used ::Kernel.require to load coderay to make it easier to stub require to simulate coderay not being available, as it allowed me to treat coderay as a collaborator. Using bare require instead forces me to stub the object-under-test, which is a bit smelly, but it fixes things so that you can get syntax highlighting without using something like bundler to manage your project and setup load paths.

Compare:

$ ruby -rubygems -e "p method(:require).source_location"
["/Users/myron/.rubies/ruby-2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb", 38]

vs

$ ruby -rubygems -e "p ::Kernel.method(:require).source_location"
nil

Merge request reports