Skip to content

Commit

Permalink
Memoize the require and construction of the StringInquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
NZKoz committed Oct 19, 2008
1 parent 550fbcc commit 6d1d48d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions railties/lib/initializer.rb
Expand Up @@ -49,8 +49,10 @@ def root
end

def env
require 'active_support/string_inquirer'
ActiveSupport::StringInquirer.new(RAILS_ENV)
@_env ||= begin

This comment has been minimized.

Copy link
@Roman2K

Roman2K Oct 19, 2008

Why the underscore?

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

def cache
Expand Down

5 comments on commit 6d1d48d

@NZKoz
Copy link
Member Author

@NZKoz NZKoz commented on 6d1d48d Oct 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Roman2K
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NZKoz: Ok, thanks.

@sentientmonkey
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Member Author

@NZKoz NZKoz commented on 6d1d48d Oct 21, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.