Null coalescing operator? What is that? In C# that’s the ??. According to the C# specs, ?? is a binary operator that evaluates first ?? second by doing

It is a nice syntactic sugar that can help you writing some nice and short code.

Is there an equivalent operator in ruby? Well I haven’t found it yet. (if there is one, please let me know) I found however a similar method in rails. It’s called presence

Example from the Api doc Instead of doing:

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

you can just do

region = params[:state].presence || params[:country].presence || 'US'

Nice and sweet!

There is one big difference however with the ?? in C#. In rails, presence uses present? behind the scene. That means that calling presence on an empty array or string would return nil.