Today I Learned

Some of the things I've learned every day since Oct 10, 2016

Category Archives: languages

125: Duck Typing

Duck typing is a term used to describe programming languages in which, when a method is called on an object, it’s never formally confirmed that the object is of a specific type acceptable as input to the method, just that the object has whatever properties are required to perform the needed operations. In other words, it isn’t formally checked what an object is but rather what it can do.

The name is a reference to the saying, “If it walks like a duck and quacks like a duck, then it’s a duck”, which echoes this same principle.

119: and, or vs &&, || (Ruby)

In the Ruby programming language, the logical operators && and || have the slight variations \texttt{and} and \texttt{or}. These plain-English operators do not behave exactly the same as their counterparts — the difference lying in their priority. While && and || have higher evaluation priority than the assignment operator \texttt{=}\texttt{and} and \texttt{or} actually have lower priority. This difference makes them useful as control-flow operators, akin to \texttt{if} or \texttt{unless}.