2012-01-24

Guess the programming language with array and dictionary addition

Guess the programming language.

  1. In which programming language are all of these true?
    • [] + [] is an empty string.
    • [] + {} is a string describing the empty object.
    • {} + [] is 0.
    • {} + {} is not-a-number.
  2. In which programming language are all of these true?
    • [] + [] is an empty list.
    • [] + {} raises TypeError.
    • {} + [] raises TypeError
    • {} + {} raises TypeError.
  3. In which programming language are all of these true?
    • [] + [] is an empty list.
    • [] + {} raises TypeError.
    • {} + [] raises NoMethodError
    • {} + {} raises NoMethodError.
  4. In which programming language are all of these true?
    • [] + [] is a positive number, e.g. 19806432.
    • [] + {} is a positive number, e.g. 20142304.
    • {} + [] is a positive number, e.g. 58300640.
    • {} + {} is a positive number, e.g. 27597024.

2012-01-16

How to put the window close button to the right on Ubuntu Lucid

This blog post explains how to put the the window close button to the top right corner of the window on Ubuntu Lucid, instead of the top left corner, where new Ubuntu versions tend to put it by default.

To put the window close button to the top right corner, and to arrange the other buttuns in the classic (2008 or earlier) layout, run the following command in a terminal window (without the leading $ sign):

$ gconftool-2 --type=string --set /apps/metacity/general/button_layout \
  menu:minimize,maximize,close

2012-01-08

C++ operator definitions inside and outside the class are not equivalent

This blog post presents some example code which demonstrates that having an operator outside a class is not equivalent to having it inside the class. As shown below, some implicit casts are applied only if the operator definition is outside the class.

It compiles when the operator definition is outside:

class C {
 public:
  C(unsigned a) {}
  C operator^(const C& masik) {
    return *this;
  }
 private:
  C();
};

int main() {
  C a = 5;
  unsigned b = 6;
  a ^ b;
  (C)b ^ a;
  b ^ a;  // Does not compile.
  return 0;
}

It compiles if the operator definition is outside:

class C {
 public:
  C(unsigned a) {}
 private:
  C();
};

C operator^(const C& a, const C& b) {
  return a;
}

int main() {
  C a = 5;
  unsigned b = 6;
  a ^ b;
  b ^ a;
  return 0;
}