Class: RuboCop::Cop::Performance::BigDecimalWithNumericArgument
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Performance::BigDecimalWithNumericArgument
- Extended by:
- AutoCorrector, TargetRubyVersion
- Defined in:
- lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
Overview
Identifies places where string argument to ‘BigDecimal` should be converted to numeric. Initializing from Integer is faster than from String for BigDecimal.
Constant Summary collapse
- MSG =
'Convert string literal to numeric and pass it to `BigDecimal`.'- RESTRICT_ON_SEND =
%i[BigDecimal to_d].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb', line 40 def on_send(node) if (string = big_decimal_with_numeric_argument?(node)) add_offense(string.source_range) do |corrector| corrector.replace(string, string.value) end elsif (string_to_d = to_d?(node)) add_offense(string_to_d.source_range) do |corrector| big_decimal_args = node.arguments.map(&:source).unshift(string_to_d.value).join(', ') corrector.replace(node, "BigDecimal(#{big_decimal_args})") end end end |