class RuboCop::Cop::Performance::BigDecimalWithNumericArgument

This cop identifies places where numeric argument to BigDecimal should be converted to string. Initializing from String is faster than from Numeric for BigDecimal.

@example

# bad
BigDecimal(1, 2)
BigDecimal(1.2, 3, exception: true)

# good
BigDecimal('1', 2)
BigDecimal('1.2', 3, exception: true)

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb, line 29
def on_send(node)
  return unless (numeric = big_decimal_with_numeric_argument?(node))
  return if numeric.float_type? && specifies_precision?(node)

  add_offense(numeric.source_range) do |corrector|
    corrector.wrap(numeric, "'", "'")
  end
end

Private Instance Methods

specifies_precision?(node) click to toggle source
# File lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb, line 40
def specifies_precision?(node)
  node.arguments.size > 1 && !node.arguments[1].hash_type?
end