Other Ruby Map Shorthand Notation

Unfortunately this shorthand notation (which calls “Symbol#to_proc”) does not have a way to pass arguments to the method or block being called, so you couldn’t even do the following: array_of_strings.map(&:include, ‘l’) #=> this would fail BUT, you are in luck because you don’t actually need this shortcut to do what you are trying to do. … Read more

Which “if” construct is faster – statement or ternary operator?

There’s only one type of “if” statement there. The other is a conditional expression. As to which will perform better: they could compile to the same bytecode, and I would expect them to behave identically – or so close that you definitely wouldn’t want to choose one over the other in terms of performance. Sometimes … Read more

PHP shorthand for isset()? [duplicate]

Update for PHP 7 (thanks shock_gone_wild) PHP 7 introduces the null coalescing operator which simplifies the below statements to: $var = $var ?? “default”; Before PHP 7 No, there is no special operator or special syntax for this. However, you could use the ternary operator: $var = isset($var) ? $var : “default”; Or like this: … Read more

Omitting the second expression when using the if-else shorthand

What you have is a fairly unusual use of the ternary operator. Usually it is used as an expression, not a statement, inside of some other operation, e.g.: var y = (x == 2 ? “yes” : “no”); So, for readability (because what you are doing is unusual), and because it avoids the “else” that … Read more