提交 051e3490 编写于 作者: R Ryuta Kamizono

Prefer faster `str.start_with?` over `str.first ==`

```ruby
str = "abc"

Benchmark.ips do |x|
  x.report("start_with?") { str.start_with?("a") }
  x.report("first ==")    { str.first == "a" }
end
```

```
Warming up --------------------------------------
         start_with?   282.381k i/100ms
            first ==   207.305k i/100ms
Calculating -------------------------------------
         start_with?     10.239M (± 2.2%) i/s -     51.393M in   5.022151s
            first ==      4.593M (± 4.5%) i/s -     23.011M in   5.021434s
```
上级 b803ed01
......@@ -11,7 +11,7 @@ module Translation
# to translate many keys within the same controller / action and gives you a
# simple framework for scoping them consistently.
def translate(key, **options)
if key.to_s.first == "."
if key.to_s.start_with?(".")
path = controller_path.tr("/", ".")
defaults = [:"#{path}#{key}"]
defaults << options[:default] if options[:default]
......
......@@ -89,7 +89,7 @@ def assert_generates(expected_path, options, defaults = {}, extras = {}, message
expected_path = uri.path.to_s.empty? ? "/" : uri.path
end
else
expected_path = "/#{expected_path}" unless expected_path.first == "/"
expected_path = "/#{expected_path}" unless expected_path.start_with?("/")
end
# Load routes.rb if it hasn't been loaded.
......@@ -210,7 +210,7 @@ def recognized_request_for(path, extras = {}, msg)
request.path = uri.path.to_s.empty? ? "/" : uri.path
end
else
path = "/#{path}" unless path.first == "/"
path = "/#{path}" unless path.start_with?("/")
request.path = path
end
......
......@@ -123,7 +123,7 @@ def localize(object, **options)
private
def scope_key_by_partial(key)
stringified_key = key.to_s
if stringified_key.first == "."
if stringified_key.start_with?(".")
if @virtual_path
@_scope_key_by_partial_cache ||= {}
@_scope_key_by_partial_cache[@virtual_path] ||= @virtual_path.gsub(%r{/_?}, ".")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册