class RuboCop::Cop::Performance::FixedSize
waldo.size
waldo = { a: corge, b: grault }
{ a: corge, **grault }.length
# good
garply.size
garply = [1, 2, 3]
[1, 2, *thud].count
# good
CONST = :baz.length
:“#{fred}”.size
# good
qux.length
bar.count
foo.size
# good
{ a: corge, b: grault }.length
# bad
# Hash methods
%W(1, 2, bar).size
[1, 2, thud].count
# bad
# Array methods
:‘baz’.length
:fred.size
# bad
# Symbol methods<br><br>%(qux).length<br>%q.count
’foo’.size
# bad
# String methods
@example
Do not compute the size of statically sized objects.
def allowed_argument?(arg)
def allowed_argument?(arg) arg && non_string_argument?(arg.first) end
def allowed_parent?(node)
def allowed_parent?(node) node && (node.casgn_type? || node.block_type?) end
def allowed_variable?(var)
def allowed_variable?(var) contains_splat?(var) || contains_double_splat?(var) end
def contains_double_splat?(node)
def contains_double_splat?(node) return false unless node.hash_type? node.each_child_node(:kwsplat).any? end
def contains_splat?(node)
def contains_splat?(node) return false unless node.array_type? node.each_child_node(:splat).any? end
def non_string_argument?(node)
def non_string_argument?(node) node && !node.str_type? end
def on_send(node)
def on_send(node) return if node.ancestors.any? { |ancestor| allowed_parent?(ancestor) } counter(node) do |var, arg| return if allowed_variable?(var) || allowed_argument?(arg) add_offense(node) end end