You may be already using the Capture Operator
& for capturing functions or quickly creating anonymous function.
For this later case, you may also know that it’s not possible to use it for creating an anonymous function that return a constant or a literal or in other words without using a placeholder in the capture expression as explained in the doc:
The only restrictions when creating anonymous functions is that at least one placeholder must be present, i.e. it must contain at least
&1, and that block expressions are not supported:
# No placeholder, fails to compile. &(:foo) # Block expression, fails to compile. &(&1; &2)
However it might be useful sometimes to return an atom or a constant or any literal.
Well you can bypass the parameter using
|| (the or operator):
&(:foo || &1) &(true || &1) &(42 || &1)
And if you want to return no matter what
nil, well you can do it too, just use
&& (the and operator):
&(false || &1) &(nil || &1)
Have a good day everyone…