Method method_missing
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def method_missing(method, *args, &block)
# puts @excel.class, method, args.inspect
if @excel.respond_to?(method)
@excel.send(method, *args, &block)
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
When using method_missing
, define respond_to_missing?
. Open
def method_missing(method, *args, &block)
# puts @excel.class, method, args.inspect
if @excel.respond_to?(method)
@excel.send(method, *args, &block)
- Read upRead up
- Exclude checks
This cop checks for the presence of method_missing
without also
defining respond_to_missing?
and falling back on super
.
Example:
#bad
def method_missing(name, *args)
# ...
end
#good
def respond_to_missing?(name, include_private)
# ...
end
def method_missing(name, *args)
# ...
super
end
Avoid comparing a variable with multiple items in a conditional, use Array#include?
instead. Open
if method == :open || method == 'open'
@excel = @excel_class.send(method, *args, &block)
else
@excel_class.send(method, *args, &block)
end
- Read upRead up
- Exclude checks
This cop checks against comparing a variable with multiple items, where
Array#include?
could be used instead to avoid code repetition.
Example:
# bad
a = 'a'
foo if a == 'a' || a == 'b' || a == 'c'
# good
a = 'a'
foo if ['a', 'b', 'c'].include?(a)
Do not place comments on the same line as the class
keyword. Open
class Excel # < BasicObject
- Read upRead up
- Exclude checks
This cop checks for comments put on the same line as some keywords.
These keywords are: begin
, class
, def
, end
, module
.
Note that some comments (such as :nodoc:
and rubocop:disable
) are
allowed.
Example:
# bad
if condition
statement
end # end if
# bad
class X # comment
statement
end
# bad
def x; end # comment
# good
if condition
statement
end
# good
class X # :nodoc:
y
end
When using method_missing
, define respond_to_missing?
and fall back on super
. Open
def self.method_missing(method, *args, &block)
@excel_class.send(method, *args, &block)
end
- Read upRead up
- Exclude checks
This cop checks for the presence of method_missing
without also
defining respond_to_missing?
and falling back on super
.
Example:
#bad
def method_missing(name, *args)
# ...
end
#good
def respond_to_missing?(name, include_private)
# ...
end
def method_missing(name, *args)
# ...
super
end