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

前端 未结 7 2367
無奈伤痛
無奈伤痛 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:10

    A simple example where the operator checks if player's id is 1 and sets enemy id depending on the result

    player_id=1
    ....
    player_id==1? enemy_id=2 : enemy_id=1
    # => enemy=2
    

    And I found a post about to the topic which seems pretty helpful.

    0 讨论(0)
提交回复
热议问题