提交 c65864cd 编写于 作者: R Ryuta Kamizono

Prefer no allocation `start/end_with?` over `String#[] ==`

上级 97c3a560
......@@ -170,7 +170,7 @@ def resolve_directories(wildcard_dependencies)
def explicit_dependencies
dependencies = source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
wildcards, explicits = dependencies.partition { |dependency| dependency[-1] == "*" }
wildcards, explicits = dependencies.partition { |dependency| dependency.end_with?("*") }
(explicits + resolve_directories(wildcards)).uniq
end
......
......@@ -46,7 +46,7 @@ def local_variable(path)
as.to_sym
else
begin
base = path[-1] == "/" ? "" : File.basename(path)
base = path.end_with?("/") ? "" : File.basename(path)
raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/
$1.to_sym
end
......
......@@ -138,7 +138,7 @@ def initialize(source, identifier, handler, format: nil, variant: nil, locals: n
@virtual_path = virtual_path
@variable = if @virtual_path
base = @virtual_path[-1] == "/" ? "" : ::File.basename(@virtual_path)
base = @virtual_path.end_with?("/") ? "" : ::File.basename(@virtual_path)
base =~ /\A_?(.*?)(?:\.\w+)*\z/
$1.to_sym
end
......
......@@ -200,7 +200,7 @@ behavior out of the box:
attr_accessor :first_name, :last_name
validates_each :first_name, :last_name do |record, attr, value|
record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
record.errors.add attr, "starts with z." if value.start_with?("z")
end
end
......
......@@ -15,7 +15,7 @@ module ActiveModel
# attr_accessor :first_name, :last_name
#
# validates_each :first_name, :last_name do |record, attr, value|
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
# record.errors.add attr, "starts with z." if value.start_with?("z")
# end
# end
#
......@@ -61,7 +61,7 @@ module ClassMethods
# attr_accessor :first_name, :last_name
#
# validates_each :first_name, :last_name, allow_blank: true do |record, attr, value|
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
# record.errors.add attr, "starts with z." if value.start_with?("z")
# end
# end
#
......
......@@ -14,7 +14,7 @@ def type
def cast(value)
case value
when ::String
if value[0] == "(" && value[-1] == ")"
if value.start_with?("(") && value.end_with?(")")
value = value[1...-1]
end
cast(value.split(","))
......
......@@ -18,7 +18,7 @@ def cast(value)
when ::String
return if value.blank?
if value[0] == "(" && value[-1] == ")"
if value.start_with?("(") && value.end_with?(")")
value = value[1...-1]
end
x, y = value.split(",")
......
......@@ -67,12 +67,12 @@ def type_cast_single_for_database(value)
end
def extract_bounds(value)
from, to = value[1..-2].split(",")
from, to = value[1..-2].split(",", 2)
{
from: (value[1] == "," || from == "-infinity") ? infinity(negative: true) : from,
to: (value[-2] == "," || to == "infinity") ? infinity : to,
exclude_start: (value[0] == "("),
exclude_end: (value[-1] == ")")
from: (from == "" || from == "-infinity") ? infinity(negative: true) : from,
to: (to == "" || to == "infinity") ? infinity : to,
exclude_start: value.start_with?("("),
exclude_end: value.end_with?(")")
}
end
......
# frozen_string_literal: true
require "active_support/core_ext/symbol/starts_ends_with"
module ActiveSupport
# Wrapping an array in an +ArrayInquirer+ gives a friendlier way to check
# its string-like contents:
......@@ -34,11 +36,11 @@ def any?(*candidates)
private
def respond_to_missing?(name, include_private = false)
(name[-1] == "?") || super
name.end_with?("?") || super
end
def method_missing(name, *args)
if name[-1] == "?"
if name.end_with?("?")
any?(name[0..-2])
else
super
......
# frozen_string_literal: true
require "active_support/core_ext/symbol/starts_ends_with"
module ActiveSupport
# Wrapping a string in this class gives you a prettier way to test
# for equality. The value returned by <tt>Rails.env</tt> is wrapped
......@@ -19,11 +21,11 @@ module ActiveSupport
class StringInquirer < String
private
def respond_to_missing?(method_name, include_private = false)
(method_name[-1] == "?") || super
method_name.end_with?("?") || super
end
def method_missing(method_name, *arguments)
if method_name[-1] == "?"
if method_name.end_with?("?")
self == method_name[0..-2]
else
super
......
......@@ -109,7 +109,7 @@ def find_in(dir)
results = {}
Dir.glob("#{dir}/*") do |item|
next if File.basename(item)[0] == ?.
next if File.basename(item).start_with?(".")
if File.directory?(item)
results.update(find_in(item))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册