Did you know that in Ruby you can compare classes with ‘less-than’ and ‘greater-than’ operators? I did not (until today). Observe:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
This operator returns a boolean value (as one would expect), so you can write code like this:
1
|
|
Note how it resembles the class definition syntax. I think that this is simply brilliant (not very intuitive, though. I wouldn’t think of writing this code).
Now that we know this fact, let’s try to sort the classes :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Now this class hierarchy is sorted, with children first and parents last. Homework: come up with a practical application for this :)