class RuboCop::Cop::Style::FloatDivision
a.fdiv(b)
# good
a.to_f / b.to_f
a.to_f / b
a / b.to_f
# bad
@example EnforcedStyle: fdiv
a / b.to_f
# good
a.to_f / b.to_f
a.to_f / b
# bad
@example EnforcedStyle: right_coerce
a.to_f / b
# good
a.to_f / b.to_f
a / b.to_f
# bad
@example EnforcedStyle: left_coerce
a / b.to_f
a.to_f / b
# good
a.to_f / b.to_f
# bad
@example EnforcedStyle: single_coerce (default)
This cop also provides other options for code consistency.
It is recommended to either always use ‘fdiv` or coerce one side only.
This cop checks for division with integers coerced to floats.
def message(_node)
def message(_node) MESSAGES[style] end
def offense_condition?(node)
def offense_condition?(node) case style when :left_coerce right_coerce?(node) when :right_coerce left_coerce?(node) when :single_coerce both_coerce?(node) when :fdiv any_coerce?(node) else false end end
def on_send(node)
def on_send(node) add_offense(node) if offense_condition?(node) end