Are there more elegant ways to prevent negative numbers in Ruby?

后端 未结 5 2136
一向
一向 2021-01-17 23:53

Given that I\'d like to do the following calculation:

total = subtotal - discount

Because discount might be greater than

5条回答
  •  醉酒成梦
    2021-01-18 00:25

    A plain if statement might be easier to understand:

    def total
      if discount > subtotal
        0
      else
        subtotal - discount
      end
    end
    

提交回复
热议问题