How do I use the conditional operator (? :) in Ruby?

前端 未结 7 2522
無奈伤痛
無奈伤痛 2020-11-22 10:34

How is the conditional operator (? :) used in Ruby?

For example, is this correct?

<% question = question.size > 20 ? question.ques         


        
7条回答
  •  一向
    一向 (楼主)
    2020-11-22 11:09

    The code condition ? statement_A : statement_B is equivalent to

    if condition == true
      statement_A
    else
      statement_B
    end
    

提交回复
热议问题