8000 Remove assumption about a stable $: · halfdan/coderay@ac2d6f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac2d6f1

Browse files
committed
Remove assumption about a stable $:
In some environments (e.g. [1]) $: can change between loading the library and using it. To avoid this problem, we always pass an absolute path to autoload internal modules. [1] pry/pry#280
1 parent 3d80f03 commit ac2d6f1

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

lib/coderay.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,18 @@ module CodeRay
127127

128128
$CODERAY_DEBUG ||= false
129129

130-
require 'coderay/version'
130+
# Assuming the rel_path is a subpath of lib/
131+
def self.abs_path(rel_path)
132+
File.join(File.dirname(__FILE__), rel_path)
133+
end
134+
135+
# In order to work in environments that alter $:, we need to
136+
# set the absolute path when autoloading files.
137+
def self.autoload(const_name, rel_path)
138+
super const_name, abs_path(rel_path)
139+
end
140+
141+
require abs_path('coderay/version')
131142

132143
# helpers
133144
autoload :FileType, 'coderay/helpers/file_type'

lib/coderay/encoders/html.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ class HTML < Encoder
109109
:hint => false,
110110
}
111111

112-
autoload :Output, 'coderay/encoders/html/output'
113-
autoload :CSS, 'coderay/encoders/html/css'
114-
autoload :Numbering, 'coderay/encoders/html/numbering'
112+
autoload :Output, CodeRay.abs_path('coderay/encoders/html/output')
113+
autoload :CSS, CodeRay.abs_path('coderay/encoders/html/css')
114+
autoload :Numbering, CodeRay.abs_path('coderay/encoders/html/numbering')
115115

116116
attr_reader :css
117117

lib/coderay/scanner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,4 @@ def scan_rest
320320
end
321321

322322
end
323-
end
323+
end

lib/coderay/scanners/java.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Java < Scanner
66

77
register_for :java
88

9-
autoload :BuiltinTypes, 'coderay/scanners/java/builtin_types'
9+
autoload :BuiltinTypes, CodeRay.abs_path('coderay/scanners/java/builtin_types')
1010

1111
# http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
1212
KEYWORDS = %w[

lib/coderay/scanners/ruby.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class Ruby < Scanner
1313
register_for :ruby
1414
file_extension 'rb'
1515

16-
autoload :Patterns, 'coderay/scanners/ruby/patterns'
17-
autoload :StringState, 'coderay/scanners/ruby/string_state'
16+
autoload :Patterns, CodeRay.abs_path('coderay/scanners/ruby/patterns')
17+
autoload :StringState, CodeRay.abs_path('coderay/scanners/ruby/string_state')
1818

1919
def interpreted_string_state
2020
StringState.new :string, true, '"'

0 commit comments

Comments
 (0)
0