Class: RuboCop::Cop::Style::ComparableBetween
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/style/comparable_between.rb
Overview
Checks for logical comparison which can be replaced with ‘Comparable#between?`.
NOTE: ‘Comparable#between?` is on average slightly slower than logical comparison, although the difference generally isn’t observable. If you require maximum performance, consider using logical comparison.
Constant Summary collapse
- MSG =
'Prefer `%<prefer>s` over logical comparison.'
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #logical_comparison_between_by_max_first?(node) ⇒ Object
- #logical_comparison_between_by_min_first?(node) ⇒ Object
- #on_and(node) ⇒ Object
Methods included from AutoCorrector
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
permalink #logical_comparison_between_by_max_first?(node) ⇒ Object
[View source]
38 39 40 41 42 43 44 |
# File 'lib/rubocop/cop/style/comparable_between.rb', line 38 def_node_matcher :logical_comparison_between_by_max_first?, <<~PATTERN (and (send {$_value :<= $_max | $_max :>= $_value}) (send {$_value :>= $_min | $_min :<= $_value})) PATTERN |
permalink #logical_comparison_between_by_min_first?(node) ⇒ Object
[View source]
29 30 31 32 33 34 35 |
# File 'lib/rubocop/cop/style/comparable_between.rb', line 29 def_node_matcher :logical_comparison_between_by_min_first?, <<~PATTERN (and (send {$_value :>= $_min | $_min :<= $_value}) (send {$_value :<= $_max | $_max :>= $_value})) PATTERN |
permalink #on_and(node) ⇒ Object
[View source]
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rubocop/cop/style/comparable_between.rb', line 46 def on_and(node) logical_comparison_between_by_min_first?(node) do |*args| min_and_value, max_and_value = args.each_slice(2).to_a register_offense(node, min_and_value, max_and_value) end logical_comparison_between_by_max_first?(node) do |*args| max_and_value, min_and_value = args.each_slice(2).to_a register_offense(node, min_and_value, max_and_value) end end |