Byte Friendly

Random thoughts on programming and related topics.

Fun With Classes: Find Which Class Is the Biggest :)

| Comments

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
class A
end

class B < A
end

class C < A
end

class D < B
end

A < B # false
A > B # true
D < A # true
D < B # true
D < C # nil
C > D # nil

This operator returns a boolean value (as one would expect), so you can write code like this:

1
puts "B inherits from A" if B < A

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
class A
end

class B < A
end

class C < A
end

class D < B
end

class E < A
end

class Class
  # we need to define the spaceship operator for classes, since it's not defined yet.
  def <=> other
    return 0 if self == other
    return -1 if self < other
    1
  end
end

klasses = [A, B, C, D, E]

klasses.sort # [E, C, D, B, A]

Now this class hierarchy is sorted, with children first and parents last. Homework: come up with a practical application for this :)

Comments