class RuboCop::Cop::Performance::BigDecimalWithNumericArgument


4.to_d(6)
BigDecimal(1, 2)
# good
BigDecimal(‘4’, 6)
BigDecimal(‘1’, 2)
# bad
BigDecimal(‘4.5’, 6, exception: true)
BigDecimal(‘1.2’, 3, exception: true)
# good
4.5.to_d(6, exception: true)
BigDecimal(1.2, 3, exception: true)
# bad
@example
suggestions from this cop are not unlikely to result in code that performs worse than before.
and if it is an Integer or a Float. Since this is very specific to ‘bigdecimal` internals,
and Number differ between versions. Additionally, performance depends on the size of the Number,
NOTE: This cop is disabled by default because the performance of initializing with a String
an integer. Initializing from Integer is faster than from String for BigDecimal.
Also identifies places where an integer string argument to BigDecimal should be converted to
Initializing from String is faster than from Float for BigDecimal.
Identifies places where a float argument to BigDecimal should be converted to a string.

def on_send(node)

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
def on_send(node)
  if (numeric = big_decimal_with_numeric_argument(node))
    if numeric.numeric_type?
      add_offense(numeric, message: MSG_FROM_FLOAT_TO_STRING) do |corrector|
        corrector.wrap(numeric, "'", "'")
      end
    elsif numeric.value.match?(/\A\d+\z/)
      add_offense(numeric, message: MSG_FROM_INTEGER_TO_STRING) do |corrector|
        corrector.replace(numeric, numeric.value)
      end
    end
  elsif (numeric_to_d = to_d(node))
    if numeric_to_d.numeric_type?
      add_offense(numeric_to_d, message: MSG_FROM_FLOAT_TO_STRING) do |corrector|
        big_decimal_args = node.arguments.map(&:source).unshift("'#{numeric_to_d.source}'").join(', ')
        corrector.replace(node, "BigDecimal(#{big_decimal_args})")
      end
    elsif numeric_to_d.value.match?(/\A\d+\z/)
      add_offense(numeric_to_d, message: MSG_FROM_INTEGER_TO_STRING) do |corrector|
        corrector.replace(node, "#{numeric_to_d.value}.to_d")
      end
    end
  end
end