is a microblog
home archives search feed blogroll

The Mysterious T Square Operator

I came across this bit of ruby code.

q ||= r

What is that?

If q is falsy (0, nil or false), then q = r. The expended form is q = q || r.

It’s similar to the null-coalescent operator in C#. The syntax is ?? instead of ||=.

this.name = name ?? "Nameless";

I like that it exists. It’s shorter than the code below, but I would’ve preferred a more obvious syntax. ||= and ?? are too cryptic.

if (!String.IsNullOrEmpty(name))
  this.name = name;
  this.name = "Nameless";
Posted on 2018-02-05   #ruby     #csharp  

← Next post    ·    Previous post →