From 44f785357cc001230215dea09bee6738e5612c63 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 22 Jul 2013 13:49:50 +0900 Subject: Use Ruby 2.0 caller_locations instead of caller if available * we no more have to manipulate the each caller strings by ourselves using caller_locations * caller_locations runs slightly faster, and creates less objects than good old caller Benchmark (loading an Engine 1000 times): caller: 262.89 ms caller_locations: 186.068 ms --- railties/lib/rails/engine.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 8000fc3b1e..be8af5c46c 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -351,8 +351,13 @@ module Rails Rails::Railtie::Configuration.eager_load_namespaces << base base.called_from = begin - # Remove the line number from backtraces making sure we don't leave anything behind - call_stack = caller.map { |p| p.sub(/:\d+.*/, '') } + call_stack = if Kernel.respond_to?(:caller_locations) + caller_locations.map(&:path) + else + # Remove the line number from backtraces making sure we don't leave anything behind + caller.map { |p| p.sub(/:\d+.*/, '') } + end + File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] }) end end -- cgit v1.2.3