Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use require_relative inplace of standard require if available. #502

Merged
merged 1 commit into from

2 participants

@ileitch

As per discussion on the mailing list: http://rubyforge.org/pipermail/rspec-users/2011-November/020760.html

Possibly the simplest approach I could think of. Provided this is OK, it could probably be moved into its own file.

@dchelimsky dchelimsky merged commit 002f6b7 into rspec:master
@dchelimsky dchelimsky referenced this pull request from a commit
@dchelimsky dchelimsky changelog and benchmark for #502
Using require_relative instead of require results in a roughly 12%
improvement in the time it takes to load rspec-core's own lib files
c59cdf8
@dchelimsky
Owner

@ileitch - this seems good to me with defined? rather than Kernel.respond_to? (feels cleaner, and turns out to shave off a millionth of a second to boot!).

Once concern I have is that we're doing this in the global namespace. What do you think about something like:

module RSpec
  if defined?(require_relative)
    def _require(path)
      require_relative path
    end
  else
    def _require(path)
      require "rspec/#{path}"
    end
  end
  module_function :_require
end

RSpec._require 'core/filter_manager'
# etc

I'd want the _ prefix or similar to identify it for readers as internal to rspec, and avoid collisions with Kernel methods.

As we add this to the other libs, we'd do something more like:

module RSpec
  unless defined?(_require)
    if defined?(require_relative)
      def _require(path)
        require_relative path
      end
    else
      def _require(path)
        require "rspec/#{path}"
      end
    end
  end
  module_function :_require
end

That way we wouldn't be redefining it as each lib got loaded.

Thoughts?

@ileitch

namespacing and defined? both look sound to me.

@dchelimsky
Owner

Let's do it. So we need to make this change in rspec-core and then add the whole thing to the other rspec libs. Would you like to do any of that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 29 deletions.
  1. +39 −29 lib/rspec/core.rb
View
68 lib/rspec/core.rb
@@ -1,32 +1,42 @@
-require 'rspec/core/filter_manager'
-require 'rspec/core/dsl'
-require 'rspec/core/extensions'
-require 'rspec/core/load_path'
-require 'rspec/core/deprecation'
-require 'rspec/core/backward_compatibility'
-require 'rspec/core/reporter'
+if Kernel.respond_to?(:require_relative)
+ def require_rspec(path)
+ require_relative path
+ end
+else
+ def require_rspec(path)
+ require "rspec/#{path}"
+ end
+end
+
+require_rspec 'core/filter_manager'
+require_rspec 'core/dsl'
+require_rspec 'core/extensions'
+require_rspec 'core/load_path'
+require_rspec 'core/deprecation'
+require_rspec 'core/backward_compatibility'
+require_rspec 'core/reporter'
-require 'rspec/core/metadata_hash_builder'
-require 'rspec/core/hooks'
-require 'rspec/core/subject'
-require 'rspec/core/let'
-require 'rspec/core/metadata'
-require 'rspec/core/pending'
+require_rspec 'core/metadata_hash_builder'
+require_rspec 'core/hooks'
+require_rspec 'core/subject'
+require_rspec 'core/let'
+require_rspec 'core/metadata'
+require_rspec 'core/pending'
-require 'rspec/core/world'
-require 'rspec/core/configuration'
-require 'rspec/core/project_initializer'
-require 'rspec/core/option_parser'
-require 'rspec/core/drb_options'
-require 'rspec/core/configuration_options'
-require 'rspec/core/command_line'
-require 'rspec/core/drb_command_line'
-require 'rspec/core/runner'
-require 'rspec/core/example'
-require 'rspec/core/shared_example_group'
-require 'rspec/core/example_group'
-require 'rspec/core/version'
-require 'rspec/core/errors'
+require_rspec 'core/world'
+require_rspec 'core/configuration'
+require_rspec 'core/project_initializer'
+require_rspec 'core/option_parser'
+require_rspec 'core/drb_options'
+require_rspec 'core/configuration_options'
+require_rspec 'core/command_line'
+require_rspec 'core/drb_command_line'
+require_rspec 'core/runner'
+require_rspec 'core/example'
+require_rspec 'core/shared_example_group'
+require_rspec 'core/example_group'
+require_rspec 'core/version'
+require_rspec 'core/errors'
module RSpec
autoload :Matchers, 'rspec/matchers'
@@ -91,5 +101,5 @@ module Core
end
end
-require 'rspec/core/backward_compatibility'
-require 'rspec/monkey'
+require_rspec 'core/backward_compatibility'
+require_rspec 'monkey'
Something went wrong with that request. Please try again.