diff options
-rw-r--r-- | chef-config/lib/chef-config/path_helper.rb | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/chef-config/lib/chef-config/path_helper.rb b/chef-config/lib/chef-config/path_helper.rb index 570e666e10..7c959f71e7 100644 --- a/chef-config/lib/chef-config/path_helper.rb +++ b/chef-config/lib/chef-config/path_helper.rb @@ -55,18 +55,24 @@ module ChefConfig end end - def self.join(*args) - path_separator_regex = Regexp.escape(File::SEPARATOR) - unless path_separator == File::SEPARATOR - path_separator_regex << Regexp.escape(path_separator) - end + def self.path_separator_regex + @path_separator_regex ||= + begin + path_separator_regex = Regexp.escape(File::SEPARATOR) + unless path_separator == File::SEPARATOR + path_separator_regex << Regexp.escape(path_separator) + end + path_separator_regex + end + end - trailing_slashes = /[#{path_separator_regex}]+$/ - leading_slashes = /^[#{path_separator_regex}]+/ + TRAILING_SLASHES_REGEX = /[#{path_separator_regex}]+$/.freeze + LEADING_SLASHES_REGEX = /^[#{path_separator_regex}]+/.freeze + def self.join(*args) args.flatten.inject do |joined_path, component| - joined_path = joined_path.sub(trailing_slashes, "") - component = component.sub(leading_slashes, "") + joined_path = joined_path.sub(TRAILING_SLASHES_REGEX, "") + component = component.sub(LEADING_SLASHES_REGEX, "") joined_path + "#{path_separator}#{component}" end end |