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
6 railties/lib/initializer.rb
@@ -49,8 +49,10 @@ def root
def env
- require 'active_support/string_inquirer'
+ @_env ||= begin
Roman2K Oct 19, 2008

Why the underscore?

+ require 'active_support/string_inquirer'
+ end
def cache

5 comments on commit 6d1d48d

Ruby on Rails member

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


@NZKoz: Ok, thanks.


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.


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.

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.