Learn Ruby
If Else
7.12
Lesson

ternary operator

Typically we write if else condition like this.

We can also write that code as shown below.

As we can see this style of writing makes code much simpler. In this case the question mark is acting like a ternary operator.

We write condition before the ?. If the condition is true then item before : is returned. And if the condition is false then item after : is returned.

Let’s see one more example.