Permalink
Browse files

Memoize the require and construction of the StringInquirer

  • Loading branch information...
1 parent 550fbcc commit 6d1d48de6a16b0ff4e4533cccf00c0ec747feefb @NZKoz NZKoz committed Oct 19, 2008
Showing with 4 additions and 2 deletions.
  1. +4 −2 railties/lib/initializer.rb
View
6 railties/lib/initializer.rb
@@ -49,8 +49,10 @@ def root
end
def env
- require 'active_support/string_inquirer'
- ActiveSupport::StringInquirer.new(RAILS_ENV)
+ @_env ||= begin
@Roman2K
Roman2K Oct 19, 2008

Why the underscore?

+ require 'active_support/string_inquirer'
+ ActiveSupport::StringInquirer.new(RAILS_ENV)
+ end
end
def cache

5 comments on commit 6d1d48d

@NZKoz
Ruby on Rails member

Just a habit really, nothing ever references @env directly so it’s purely an implementation detail for within that method.

@Roman2K

@NZKoz: Ok, thanks.

@sentientmonkey

Have you seen ticket #1158

This should resolve the performance issue, although I’m still unsure as why the require is inside the helper method – seems like a really strange pattern to follow.

@Roman2K

@sentientmonkey
Why do you find it strange that the require is inside the method body? It makes perfect sense to me: only load the StringInquirer class when it’s actually needed. Also, it helps keeping track of which require is used for what. However, it should be taken out of the method body as soon as the class is used somewhere else within the same file.

@NZKoz
Ruby on Rails member

The reason for the require inside the method is described in the ticket you’ve linked to. it is kinda strange but the specific case it was needed for is kinda strange too.

Please sign in to comment.