I would like to know the difference between the if else syntax and the ? syntax. I saw an example on the web where they used a question mark instead of an if else statement. I understand that if statements can be used on only one line
return true if 4 > 3
instead of
if 4 > 3
return true
else
return false
end
However, I'm confused about when you'd use a question mark in that case. Here is the example, I saw in a video.
@date = params[:month] ? Date.parse(params[:month]) : Date.today
On the surface this appears to be an alternative syntax for the if else statement. However, I've never seen it before and have no idea what it does.
I also know that methods can end in a question mark, indicating it should return either a true or false value like so
def is_multiple_of_4? (x)
return true if x % 4 == 0
end
However, the above syntax is different. I would love to learn what that means and what the difference is between that and if else statements.