Ternary expression with “defined?” returns “expression” instead of value?

前端 未结 3 920
抹茶落季
抹茶落季 2021-01-18 04:53

I\'m pretty new to Ruby and Rails but even after searching stack overflow and google I couldn\'t find an answer to this.

I\'ve got a simple Ruby shorthand if stateme

3条回答
  •  无人共我
    2021-01-18 05:46

    You're looking for the null coalescing operator. Try this:

    @c = amount || r(1,4)
    

    This code will assign amount to @c if amount is defined. If not it will assign the result of r(1,4) to @c.

    http://eddiema.ca/2010/07/07/the-null-coalescing-operator-c-ruby-js-python/

提交回复
热议问题