Why does Ruby have both private and protected methods?

protected methods can be called by any instance of the defining class or its subclasses. private methods can be called only from within the calling object. You cannot access another instance’s private methods directly. Here is a quick practical example: def compare_to(x) self.some_method <=> x.some_method end some_method cannot be private here. It must be protected … Read more

Advantages of Java’s enum over the old “Typesafe Enum” pattern?

“cannot be subclassed” isn’t a restriction. It’s one of the big advantages: It ensures there there is always only ever exactly the set of values defined in the enum and no more! enum correctly handles serialization. You can do that with type-safe enums as well, but it’s often forgotten (or simply not known). This ensures … Read more

Why is adding null to a string legal?

From MSDN: In string concatenation operations, the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string. More information on the + binary operator: The binary + operator performs string concatenation when one or both operands are of type string. If … Read more

Why can’t local variable be used in GNU C basic inline asm statements?

You can use local variables in extended assembly, but you need to tell the extended assembly construct about them. Consider: #include <stdio.h> int main (void) { int data1 = 10; int data2 = 20; int result; __asm__( ” movl %[mydata1], %[myresult]\n” ” imull %[mydata2], %[myresult]\n” : [myresult] “=&r” (result) : [mydata1] “r” (data1), [mydata2] “r” … Read more

Why are many languages case sensitive?

I don’t think you’ll get a better answer than “because the author(s) of that language thought it was better that way”. Personally, I think they’re right. I’d hate to find these lines anywhere in the same source file (and refer to the same object+method)… SomeObject.SomeMethod(); … SOMEOBJECT.SOMEMETHOD(); … someObject.someMethod(); … sOmEoBjEcT.sOmEmEtHoD(); I don’t think anyone … Read more

Why does Python’s itertools.permutations contain duplicates? (When the original list has duplicates)

I can’t speak for the designer of itertools.permutations (Raymond Hettinger), but it seems to me that there are a couple of points in favour of the design: First, if you used a next_permutation-style approach, then you’d be restricted to passing in objects that support a linear ordering. Whereas itertools.permutations provides permutations of any kind of … Read more