Skip to content
This repository
Browse code

Memoize the require and construction of the StringInquirer

  • Loading branch information...
commit 6d1d48de6a16b0ff4e4533cccf00c0ec747feefb 1 parent 550fbcc
Michael Koziarski authored October 19, 2008

Showing 1 changed file with 4 additions and 2 deletions. Show diff stats Hide diff stats

  1. 6  railties/lib/initializer.rb
6  railties/lib/initializer.rb
@@ -49,8 +49,10 @@ def root
49 49
50 50
51 51
     def env
-      require 'active_support/string_inquirer'
+      @_env ||= begin
+        require 'active_support/string_inquirer'
+      end
54 56
55 57
56 58
     def cache

5 notes on commit 6d1d48d

Michael Koziarski

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

Roman Le Négrate

@NZKoz: Ok, thanks.

Scott Windsor

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.

Roman Le Négrate

@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.

Michael Koziarski

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.
Something went wrong with that request. Please try again.