question mark and colon - if else in ruby
?:
is a ternary operator that is present in many languages. It has the following syntax:
expression ? value_if_true : value_if_false
In Ruby, it is a shorter version of this:
if expression
value_if_true
else
value_if_false
end
This is your code, rearranged for easier understanding.
def sort_column
cond = Product.column_names.include?(params[:sort])
cond ? params[:sort] : "name"
# it's equivalent to this
# if cond
# params[:sort]
# else
# 'name'
# end
end
First question mark is part of a method name, the second one - part of ternary operator (which you should read about).