Difference between “and” and && in Ruby?

and is the same as && but with lower precedence. They both use short-circuit evaluation. WARNING: and even has lower precedence than = so you’ll usually want to avoid and. An example when and should be used can be found in the Rails Guide under “Avoiding Double Render Errors“.

Difference between “or” and || in Ruby?

It’s a matter of operator precedence. || has a higher precedence than or. So, in between the two you have other operators including ternary (? 🙂 and assignment (=) so which one you choose can affect the outcome of statements. Here’s a ruby operator precedence table. See this question for another example using and/&&. Also, …

Read more

What does =~ do in Perl?

=~ is the operator testing a regular expression match. The expression /9eaf/ is a regular expression (the slashes // are delimiters, the 9eaf is the actual regular expression). In words, the test is saying “If the variable $tag matches the regular expression /9eaf/ …” and this match occurs if the string stored in $tag contains those characters 9eaf consecutively, in order, at any point. So this …

Read more

Not equal to != and !== in PHP

== and != do not take into account the data type of the variables you compare. So these would all return true: === and !== do take into account the data type. That means comparing a string to a boolean will never be true because they’re of different types for example. These will all return false: You should compare data types for functions that …

Read more

What is the idiomatic Go equivalent of C’s ternary operator?

As pointed out (and hopefully unsurprisingly), using if+else is indeed the idiomatic way to do conditionals in Go. In addition to the full blown var+if+else block of code, though, this spelling is also used often: and if you have a block of code that is repetitive enough, such as the equivalent of int value = a <= b ? a : b, …

Read more

What is the ‘new’ keyword in JavaScript?

It does 5 things: It creates a new object. The type of this object is simply object. It sets this new object’s internal, inaccessible, [[prototype]] (i.e. __proto__) property to be the constructor function’s external, accessible, prototype object (every function object automatically has a prototype property). It makes the this variable point to the newly created object. It executes the constructor function, using the newly created …

Read more